尝试从Java运行应用程序时Java虚拟机错误

时间:2013-08-19 16:12:33

标签: java noclassdeffounderror processbuilder

我正在尝试从java应用程序运行.exe,但我收到此错误消息:

Java虚拟机启动器(标题)    发生了Java异常。

我试过了:

try {
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec("C:\\PathToExe\\MyExe.exe");
            InputStream in = p.getInputStream();
            OutputStream out = p.getOutputStream();
            InputStream err = p.getErrorStream();
} catch (Exception exc) {}

try {
            Process process = new         ProcessBuilder("C:\\PathToExe\\MyExe.exe").start();
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;            
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            Logger.getLogger(ScannerUI.class.getName()).log(Level.SEVERE, null, ex);
}

当我尝试运行像uTorrent这样的其他东西时,它们都工作,但是当我尝试运行我必须运行的.exe时,它失败了。

此外,此.exe位于一个文件夹中,如果我尝试单独运行此.exe(在其文件夹中),则会返回相同的错误消息。

我不认为上面的代码是错误的,但它缺少能够运行我的.exe的东西。

新代码:

try
        {

            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("C:\\PathToExe\\MyExe.exe");                
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<ERROR>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</ERROR>");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Exception e)
          {
            e.printStackTrace();
          }

新输出:

<ERROR>
Exception in thread "main" java.lang.NoClassDefFoundError: **org/lwjgl/LWJGLException**
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more
</ERROR>
Process exitValue: 0

1 个答案:

答案 0 :(得分:0)

引述你:

  

此外,此.exe位于文件夹中,如果我尝试单独运行此.exe   (在它的文件夹中)它返回相同的错误消息。

您很可能忘记设置适当的目录。使用ProcessBuilder代替Runtime.exec(),并在ProcessBuilder.directory(File)之前调用ProcessBuilder.start()方法。

有关详细信息,请参阅this question