尝试使用 ProcessBuilder 执行 Java 应用程序时出现类未找到异常

发布于 2024-10-31 03:42:06 字数 2328 浏览 4 评论 0原文

我试图通过使用 servlet 中的 Java ProcessBuilder 在 jar 文件中的类中执行 Main 方法。由于其他依赖项,我需要在单独的进程中运行它。

我收到以下异常,所以我猜我没有在参数数组中正确传递包和主要方法名称(com.test.Main)。我不知道如何正确地做到这一点。

我将不胜感激任何建议。谢谢。

错误 -- java.lang.NoClassDefFoundError: com/test/Main 错误 - 导致:java.lang.ClassNotFoundException:com.test.Main 错误 - 在 java.net.URLClassLoader$1.run(URLClassLoader.java:202) 错误 - 在 java.security.AccessController.doPrivileged(本机方法) 错误 - 在 java.net.URLClassLoader.findClass(URLClassLoader.java:190) 错误 - 在 java.lang.ClassLoader.loadClass(ClassLoader.java:307) 错误 - 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

错误——位于 java.lang.ClassLoader.loadClass(ClassLoader.java:248) 错误 - 找不到主类:com.test.Main。程序将退出。 错误 - 线程“main”中出现异常

这是我的代码。

    public int runProcessBuilder() throws IOException, InterruptedException{
    {
        // Get absolute path
        File dir_location = new File(".");
        String appPath = dir_location.getCanonicalPath() + "\\Tomcat 6.0\\webapps\\TestServer\\WEB-INF";

        // Args to run
        String[] argList = {"java.exe","-Djava.library.path="+appPath+"\\lib","-classpath",appPath+"\\lib\\test.jar","com.test.Main","-pTEST_ARG","123"};           

        // Create ProcessBuilder
        ProcessBuilder builder = new ProcessBuilder(argList);

        // Set Environment variable(s)
        Map<String, String> environ = builder.environment();
        environ.put("TEST_HOME", appPath);

        // Set java directory - TODO: use system property
        String java_exe = "C:\\Program Files\\Java\\jdk1.6.0_18\\bin";
        builder.directory(new File(java_exe));

        // Start Process
        final Process process = builder.start();

        // Read error stream
        StreamReader errorReader = new StreamReader(process
                .getErrorStream(), "ERROR");

        // Read input stream
        StreamReader outputReader = new StreamReader(process
                .getInputStream(), "OUTPUT");

        // Start both reader threads
        errorReader.start();
        outputReader.start();

        // Wait for process end and get Exit Code
        int exitCode = process.waitFor();
        System.out.println("Exit code: " + exitCode);

        return exitCode;
      }
}

I'm trying to execute the Main method within a class in a jar file by using Java's ProcessBuilder from a servlet. I need to run this in a separate process due to other dependencies.

I'm getting the following exception, so I guess that I am not correctly passing the package and Main method name (com.test.Main) in the arguments array. I am not sure how to do this correctly.

I'd appreciate any suggestions. Thanks.

ERROR -- java.lang.NoClassDefFoundError: com/test/Main
ERROR -- Caused by: java.lang.ClassNotFoundException: com.test.Main
ERROR -- at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
ERROR -- at java.security.AccessController.doPrivileged(Native Method)
ERROR -- at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
ERROR -- at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
ERROR -- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

ERROR -- at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
ERROR -- Could not find the main class: com.test.Main. Program will exit.
ERROR -- Exception in thread "main"

Here's my code.

    public int runProcessBuilder() throws IOException, InterruptedException{
    {
        // Get absolute path
        File dir_location = new File(".");
        String appPath = dir_location.getCanonicalPath() + "\\Tomcat 6.0\\webapps\\TestServer\\WEB-INF";

        // Args to run
        String[] argList = {"java.exe","-Djava.library.path="+appPath+"\\lib","-classpath",appPath+"\\lib\\test.jar","com.test.Main","-pTEST_ARG","123"};           

        // Create ProcessBuilder
        ProcessBuilder builder = new ProcessBuilder(argList);

        // Set Environment variable(s)
        Map<String, String> environ = builder.environment();
        environ.put("TEST_HOME", appPath);

        // Set java directory - TODO: use system property
        String java_exe = "C:\\Program Files\\Java\\jdk1.6.0_18\\bin";
        builder.directory(new File(java_exe));

        // Start Process
        final Process process = builder.start();

        // Read error stream
        StreamReader errorReader = new StreamReader(process
                .getErrorStream(), "ERROR");

        // Read input stream
        StreamReader outputReader = new StreamReader(process
                .getInputStream(), "OUTPUT");

        // Start both reader threads
        errorReader.start();
        outputReader.start();

        // Wait for process end and get Exit Code
        int exitCode = process.waitFor();
        System.out.println("Exit code: " + exitCode);

        return exitCode;
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-11-07 03:42:06

“-classpath”参数或您尝试使用的 JAR 文件有问题。

  • 打印出“-classpath”参数的值,并检查 JAR 文件是否确实位于文件系统中的该位置,并且它是可读的。

  • 使用jar -tvf test.jar | grep ... 检查 Main 类是否在 JAR 文件中,并且在 JAR 中具有正确的路径。

(问题不是清单......因为您没有使用“-jar”。)

(这也不是 main 入口点方法的签名的问题......因为会导致不同的异常。)

There is something wrong with either the "-classpath" argument or the JAR file you are trying to use.

  • Print out the value of the "-classpath" argument, and check that the JAR file is really at that location in the file system, and that it is readable.

  • Use jar -tvf test.jar | grep ... to check that the Main class is in the JAR file, and has the correct path in the JAR.

(It is not the Manifest that is the issue ... because you are not using "-jar".)

(It is also not an issue with the signature of main entrypoint method ... because that would have resulted in a different exception.)

窗影残 2024-11-07 03:42:06

您是否在 jar 的清单文件中设置了 Main-Class 值?

这将是这样的:

Main-Class: com.test.Main

您可能想要查看 Running JAR-打包软件文档

Have you set the Main-Class value in your jar's manifest file?

That would be something like:

Main-Class: com.test.Main

You might want to review the Running JAR-Packaged Software documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文