0

我花了很多时间试图找出问题所在,但仍然没有答案。所以问题来了:

我的项目中有 2 个课程:

ProcessTest
|
|-src
  |
  |-testpackage
  | |
  | |-TestClass
  |
  |-AnotherClass

我从这个项目中创建了一个 jar 文件(一个工件)。我希望我的程序只使用这个 jar 文件。

这是这两个类的代码:

public class TestClass {

    public static void main(String[] args) {

        String myPath = System.getProperty("user.dir");
        String stringToExecute = String.format("java -classpath \"%s/test.jar\" AnotherClass 0 10", myPath);

        System.out.println("Trying to execute: "+stringToExecute);

        Process process = null;
        try {
            process = Runtime.getRuntime().exec(stringToExecute);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuilder text = new StringBuilder();
        String tmp;
        try {
            while ((tmp = errReader.readLine()) != null)
                text.append(tmp + "\n");
        } catch (IOException e) {
            e.printStackTrace();  
        }
        System.out.println(process);
        System.out.println(text);
        System.out.println(process.exitValue());

    }
}

第二类:

public class AnotherClass {

    public static void main(String[] args) {
        if (args.length >= 2) {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            if (a > b) {
                System.out.println("The first argument should be less or equal than the second argument.");
            } else {
                for (int i=a; i<b; i++) {
                    System.out.println("Number "+i);
                }
            }
        } else {
            System.out.println("Not enough arguments.");
        }
    }

}

有趣的是,当我只是将 stringToExecute 复制/粘贴到终端时 - 一切正常,但是当我尝试在 TestClass 中运行代码时,我得到了:

$ java -classpath "/tmp/test.jar" testpackage.TestClass
Trying to execute: java -classpath "/tmp/test.jar" AnotherClass 0 10
java.lang.UNIXProcess@7d67d940
Exception in thread "main" java.lang.NoClassDefFoundError: AnotherClass
Caused by: java.lang.ClassNotFoundException: AnotherClass
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: AnotherClass.  Program will exit.

1
$

test.jar 包含:

$ jar tvf /tmp/test.jar 
  0 Mon Aug 27 05:19:16 EEST 2012 testpackage/
  2082 Mon Aug 27 05:19:16 EEST 2012 testpackage/TestClass.class
  1087 Mon Aug 27 05:19:16 EEST 2012 AnotherClass.class
$

谢谢你的帮助!

PS 我正在使用 IntelliJ IDEA(如果重要的话)。例如用于创建工件 (test.jar)。

4

1 回答 1

0

尝试

process = Runtime.getRuntime().exec(new String[]{"java","-cp","/tmp/test.jar", "AnotherClass", "0", "10"});

使用硬编码的“/tmp”路径并使用String数组。

于 2012-08-27T20:05:28.407 回答