Problem
- Product Name: VisiBroker
- Product Version: 7.0
- Platform: Solaris
Error on "vbj" when running a Java program that calls Runtime.exec() in Solaris.
Consider the sample Java program below:
import java.io.InputStream;
public class Test
{
public static void main(String[] args)
{
try {
String cmd = "java -classpath . HelloWorld";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
proc.waitFor();
InputStream is = proc.getInputStream();
proc.getOutputStream();
int i = is.available();
String out = null;
byte[] buffer = new byte;
if (i > 0) {
int numberBytes = is.read(buffer, 0, i);
out = new String(buffer);
}
System.out.println("Process output:" out);
proc.destroy();
}
catch (Exception e)
{
System.out.println("Exception: " e.getMessage());
}
}
}
Note: HelloWorld is a separate Java program. You will need to create one and make sure it is in the same folder as this class.
The program will run using "vbj" in most of JDK versions and in different platforms. But an issue occurs in a very rare occasions in some Solaris machines where "vbj" does not work and differs to the output when using "java".
Below are the outputs of the program using "java" and "vbj" in Solaris environment.
$ javac Test.java
$ java Test
Process output:Hello World!
$ vbj Test
Process output:null
Resolution
Root Cause
The exact cause is not known, but it is suspected that certain JDK and environment setting might be causing the problem.
Workaround
For the above program to work using "vbj", some environment variables are needed to be passed when calling Runtime.exec() function.
Below are the modified/added lines for the same sample program:
...
String []cmd = new String[]{ "java","HelloWorld" };
String []envp = new String[]{ "JAVA_HOME=/space/VB8/jdk/jdk1.5.0" }; //(JAVA_HOME needs to be set to the correct path)
...
Process proc = rt.exec(cmd,envp);
With these modifications, the "vbj" will work the same as "java" when running the sample program.
Below are the outputs:
$ javac Test.java
$ java Test
Process output:Hello World!
$ vbj Test
Process output:Hello World!
#Security
#VisiBroker
#Runtime.exec
#vbj