0

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
4

1 に答える 1

0

あなたの引用:

また、この .exe はフォルダー内にあり、この .exe を (そのフォルダーから) 単独で実行しようとすると、同じエラー メッセージが返されます。

ほとんどの場合、適切なディレクトリを設定するのを忘れています。の代わりにProcessBuilderを使用し、 の前にメソッドRuntime.exec()を呼び出します。ProcessBuilder.directory(File)ProcessBuilder.start()

詳細については、この質問を参照してください。

于 2013-08-22T22:17:05.900 に答える