0

I have two jar files named test1.jar and test2.jar. In order to execute a function in in this jar file, I run below command in cmd. “java getOutput argument1 argument2 argument3 argument4”

As a pre-requisite, I have set the following entries in my system environment variables to execute the java function using the above command.

1.CLASSPATH=C:\Folder\test1.jar;C:\Folder\test2.jar;C:\Program Files\Java\jre1.8.0_181\lib\plugin.jar

2.JAVA_BIN=C:\Program Files\Java\jre1.8.0_181\bin

3.JAVA_HOME=C:\Program Files\Java\jre1.8.0_181

4.PATH = C:\Program Files\Java\jre1.8.0_181\bin

The output of the above command is a string.

I have another java code. I have to capture the above string output in a variable in my java program and I am able to do this by using below java code in eclipse.

String strArgs= strArg1+" "+strArg2+" "+strArg3+" "+strArg4;   
Process p=Runtime.getRuntime().exec("cmd /c java  getOutput "+strArgs+"");    
BufferedReader input =new BufferedReader(new 

InputStreamReader(p.getInputStream()));                                                                                                                                                                                                                                                                                                

String strOutput = input.readLine();   
if (strOutput != null) {              
System.out.println(strOutput);
String strVariable = strOutput;                                                                                                                                          
input.close();                                                                               
}else {                                                                                              
System.out.println("FAIL- Output not generated");                                                                                                                                                  
}

In order to execute the above code on unix, I replace Line 2 with "Process p=Runtime.getRuntime().exec("usr/bin/java getOutput "+strArgs+"");" , convert the eclipse java project into a jar file named automation.jar. And I try to execute this jar file on unix using the below command . `java -cp automation.jar Package.MainClass.' When I run the code I get the output as null. But when I run the command on windows command prompt I get the output string.

Could you please help me on how to set the system variables in unix and perform the above operation to generate the string output.

Please let me know if any additional information required.

3
  • it's quite normal for that not to work on Linux. you are calling Windows applications. There are Linux alternatives, just check for them. Commented Jan 25, 2019 at 6:46
  • try replacing cmd /c with JAVA_HOME on linux : Runtime.getRuntime().exec("Java-location/bin/java getOutput argumets ... Commented Jan 25, 2019 at 6:53
  • Thank you. i get below response on the line Process p = Runtime.getRuntime().exec(java_bin + "/java getOutput " + strArgs); when i run automation.jar on unix..... java.io.IOException: Cannot run program "nulljava": error=2, No such file or directory Commented Jan 25, 2019 at 7:39

1 Answer 1

1

cmd is windows only. Unix uses a shell. But you shouldn't need either here. Just remove the cmd call.

Process p = Runtime.getRuntime().exec("java getOutput " + strArgs);

If it can't find Java, you can read the JAVA_BIN (or JAVA_HOME) from the environment.

String java_bin = System.getenv("JAVA_BIN");
Process p = Runtime.getRuntime().exec(java_bin + "/java getOutput " + strArgs);

Finally, your environment variables look like Windows path variables. Make sure they're correct for your Unix environment (Unix doesn't have a "C" drive).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. i get below response on the line Process p = Runtime.getRuntime().exec(java_bin + "/java getOutput " + strArgs); when i run automation.jar on unix..... java.io.IOException: Cannot run program "nulljava": error=2, No such file or directory
may i know how to set these variables in unix. 1.CLASSPATH=C:\Folder\test1.jar;C:\Folder\test2.jar;C:\Program Files\Java\jre1.8.0_181\lib\plugin.jar 2.JAVA_BIN=C:\Program Files\Java\jre1.8.0_181\bin 3.JAVA_HOME=C:\Program Files\Java\jre1.8.0_181 4.PATH = C:\Program Files\Java\jre1.8.0_181\bin
@Prasanna In your terminal you could do. export CLASSPATH=/opt/whatever/java_home/bin to set a variable for a particular shell session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.