我正在尝试使用以下代码运行具有本机库和内容的应用程序:
ProcessBuilder pb = new ProcessBuilder("javaw",
"-classpath",
binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;",
"-Djava.library.path=" + nativesDir,
"monster860.polyrd.Polyrd");
我尝试在命令行中进行等效操作,仅使用
bindir
而不是binDir + "polyrd.jar;" + binDir + "lwjgl.jar;" + binDir + "lwjgl_util.jar;"
将其更改为-cp,并在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。
最佳答案
从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");