Hello everyone don’t know why this code works on windows but not on linux so if anyone sees something wrong with this or another way of doing this I would really appreciate some guidance thanks for your time.
Process p = Runtime.getRuntime().exec(linuxCommand);
int cont=0,retorno=p.waitFor();
try {
synchronized (this) {
while (retorno!=0 && conteo<10000){
retorno=p.waitFor();
System.out.println("cont++);
}
if (retorno == 0) {
ans = true;
logger.info("Return Value: " + ans);
}else{
ans = false;
logger.info("Return Value: " + ans);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Objective: create *1_Output.txt* from the info contained in *1_Input.txt*,
MyObjectT resides in /var/xp/client/a/h/n/test
MyObjectT will call several classes in /var/xp/client/a/h/n/clases/z1, and eventually READ the content of 1_Output.txt so it can return ans(true/false)
PROBLEM
- on windows: the flow when to fast it did not wait for the 1_Output.txt file to be created so there was nothing to read, with current code that was solved. (p.waitFor() eventually return 0 and everyone is happy)
- Linux : p.waitFor() never returns 0 the flow ends and nothing
linuxCommand is a parsed String that represent a java command to be executed in linux
notice that if I paste this command as it is in the command shell it would run ok with no warning or error . I donw think “ play an important role here. linuxCommand->
/opt/jdk/jdk1.6.0_22/bin/java -classpath “/var/xp/client/a/h/n/clases/z1″:”/var/xp/client/a/h/n/test” MyObjectT /m:Param21 /f:”/var/xp/client/a/h/n/IOFile/1_Input.txt” /o:”/var/xp/client/a/h/n/IOFile/1_Output.txt”
so what confuses me is that if use getErrorStream() to track down some errors an I get this
Exception in thread "main" java.lang.NoClassDefFoundError: MyObjectT
Caused by: java.lang.ClassNotFoundException: MyObjectT
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MyObjectT. Program will exit.
any help, its much appreciated thank again
lol Im sorry for the variable name in spanish retorno=return but at the time I translated I did not stop and think about the return ajajjaajajajaj :=)
There’s a few things in play, simultaneously. First, you’ve got multiple programs running — this one and the one that you’re spawning — and the errors in that program are causing troubles here.
First, the
linuxCommand:I’ve arbitrarily line-wrapped it. Depending upon how you’ve defined this string in your program, you may need to escape all those
"marks in this string. (If you just print this usingSystem.out.println(), what does it look like?)Probably the better approach is to use a slightly different form of
exec(), one that takes an array of strings instead of a single string. This way you can break the parameters as the program requires without worrying about proper shell quoting. It’d look something like this (untested) chunk:You did say you’ve run this command by hand in a shell to confirm it works, but those
/i,/o, etc. feel very awkward on a Unix-alike platform. Are you confident that format works as expected on Linux?