2

我正在尝试使用以下代码运行具有本机库和内容的应用程序:

ProcessBuilder pb = new ProcessBuilder("javaw",
    "-classpath", 
    binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;",
    "-Djava.library.path=" + nativesDir,
    "monster860.polyrd.Polyrd");

我尝试在命令行中执行等效操作,将其更改为 -cp,仅使用bindir而不是binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;",并在 java 和 javaw 之间切换,但无论我做什么,它都给了我:

java.lang.NoClassDefFoundError: monster860/polyrd/Polyrd
Caused by: java.lang.ClassNotFoundException: monster860.polyrd.Polyrd
    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) 

任何人都可以帮忙吗?

我的操作系统是 Windows Vista。是的,这些文件确实存在。

这是我得到binDir和的方法nativesDir

public ProcessRunnable(File nativesDir, File binDir) {
        try {
            this.nativesDir = nativesDir.getCanonicalPath() + File.separator;
            this.binDir = binDir.getCanonicalPath() + File.separator;
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

编辑:嗯,当然,它与此完全无关,但下载器只下载文件的前 2 KB。

4

1 回答 1

1

从 Java 6 开始,“为了特别方便,包含基本名称的类路径元素*被认为等同于指定目录中所有文件的列表,扩展名为.jar.JAR”。有关详细信息,请参阅java命令行选项。

附录:此示例开始JFreeChart使用提到的通配符功能。

import java.io.BufferedReader;
import java.io.InputStreamReader;

/** @see https://stackoverflow.com/a/15121864/230513 */
public class PBTest {

    private static final String baseDir = "/opt/jfreechart/";
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-cp",
            baseDir + "lib/*:" + baseDir + "jfreechart-1.0.14-demo.jar",
            "demo.SuperDemo");
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            String s;
            // read from the process's combined stdout & stderr
            BufferedReader stdout = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = stdout.readLine()) != null) {
                System.out.println(s);
            }
            System.out.println("Exit value: " + p.waitFor());
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
}

附录:这是 Windows 的更改,需要;作为路径分隔符。

private static final String baseDir = "C:/Users/Public/JFreeChart/";
public static void main(String[] args) {
    ProcessBuilder pb = new ProcessBuilder("java", "-cp",
        baseDir + "lib/*;" + baseDir + "jfreechart-1.0.14-demo.jar",
        "demo.SuperDemo");
于 2013-02-27T20:35:28.440 回答