How to Get Java Output with Ruby

How to get java output with ruby

java -version writes to STDERR, so the following should work:

@output = stderr.read

On Linux, you can check in the shell where the output is going by supressing either STDERR or STDOUT:

java -version >/dev/null

still prints the information.

What is Java using to output in console, and how can I capture it in ruby?

Java is using STDERR to print its version information. You can capture that easily by routing STDERR to STDOUT's stream, and capturing both.

asdf = `java -version 2>&1`
puts asdf

will output:

java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-10M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)

This is a very common technique when working at the command-line. Do a man sh at the command-line, and search for REDIRECTION using "/REDIRECTION" and read from there.

How to pass parameters to a jar file and read the console output in ruby?

Put argsA and argsB in their own parameter, otherwise when seeing spaces in the parameters, ruby will surround them with quotes, so they're considered like one.

pipe = IO.popen( [ '/path/to/java', '-jar', '/path/to/jarfile.jar',"#{argsA}", "#{argsB}", {SDTERR=>STDOUT} ] 

Calling a Java program in Ruby and getting output

output = %x{java your_program.java}

Running Ruby script from Java

You need to consume the standard output and standard error streams to get output from your process and find out what is wrong. See the answer to "Using a thread to capture process output".

Getting output of system() calls in Ruby

I'd like to expand & clarify chaos's answer a bit.

If you surround your command with backticks, then you don't need to (explicitly) call system() at all. The backticks execute the command and return the output as a string. You can then assign the value to a variable like so:

output = `ls`
p output

or

printf output # escapes newline chars

Getting JRuby-internal Java object from Ruby code

Ah, I found the answer in my tries-and-errors.

The following works.

"foobar".to_java(Java::org.jruby.RubyString).getEncoding()
Time.now.to_java(Java::org.jruby.RubyTime).getDateTime()

Migrate String encryption from Ruby to Java

The first thing to do is to derive the IV and key from the given password.

From the link above, you will get an encoded IV and KEY that corresponds with "VDiJjncs4ak=" and "s9e42J3PpmQv8n5T8L3zzuFaGdrzK/wU" respectively.
This means that the key and IV vector used in the Java code are wrong as it was said in the comments.

Below is the resulting Java code:

public class SymmetricDESedeCipher {
private static final String DATA = "Whackabad";
private static final String ALGORITHM = "DESede";
private static final String XFORM = "DESede/CBC/PKCS5Padding";
private static final String KEY = "s9e42J3PpmQv8n5T8L3zzuFaGdrzK/wU";
private static final String IV = "VDiJjncs4ak=";

private static byte[] encrypt(String data,
SecretKey key, String XFORM, byte[] iv) throws Exception {
Cipher cipher = Cipher.getInstance(XFORM);
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
return cipher.doFinal(data.getBytes());
}

public static void main(String[] unused) throws Exception {
DESedeKeySpec spec = new DESedeKeySpec(new BASE64Decoder().decodeBuffer(KEY));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = secretKeyFactory.generateSecret(spec);

byte[] encBytes = encrypt(DATA, secretKey, XFORM, new BASE64Decoder().decodeBuffer(IV));

System.out.println("Data: " + DATA);
System.out.println("Encrypted Data: " + new BASE64Encoder().encode(encBytes));
}
}

Output:

Data: Whackabad
Encrypted Data: AEsDXVcgh2jsTjlDgh+REg==


Related Topics



Leave a reply



Submit