-1

大家好,大家不知道为什么这段代码可以在 Windows 上运行,但不能在 linux 上运行,所以如果有人发现这种方式或其他方式有问题,我将非常感谢您的指导,感谢您的宝贵时间。

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();
}

目标:根据 *1_Input.txt* 中包含的信息创建 *1_Output.txt*,

MyObjectT驻留在 /var/xp/client/a/h/n/test
MyObjectT会调用 /var/xp/client/a/h/n/clases/z1 中的几个类,并最终读取1_Output.txt 的内容所以它可以返回ans (true/false)

问题

  • 在 Windows 上:快速的流程没有等待创建 1_Output.txt 文件,因此没有什么可读取的,当前代码已解决。(p.waitFor() 最终返回 0 并且每个人都很高兴)
  • Linux:p.waitFor() 永远不会返回 0 流程结束并且什么都没有

linuxCommand是一个解析后的字符串,表示要在 linux 中执行的 java 命令

请注意,如果我将这个命令按原样粘贴在命令 shell 中,它将运行正常,没有警告或错误。我不认为 在这里发挥重要作用。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/客户端/a/h/n/IOFile/1_Output.txt"

所以让我感到困惑的是,如果使用 getErrorStream() 来追踪一些错误,我会得到这个

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.

任何帮助,非常感谢再次感谢

大声笑我很抱歉西班牙语 retorno=return 中的变量名,但在我翻译的时候我并没有停下来考虑返回 ajajjaajajajaj :=)

4

1 回答 1

1

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:

/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"

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 using System.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:

String 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"};
Process p = Runtime.getRuntime().exec(linuxCommand);

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?

于 2012-06-22T22:19:31.193 回答