我不明白为什么这个ClassNotFoundException被抛出 [英] I don't understand why this ClassNotFoundException gets thrown
问题描述
执行结果:/ usr / bin / java -classpath/ home / geoGeoline
错误流:
线程main中的异常java.lang.NoClassDefFoundError:Geoline
引起的: java.lang.ClassNotFoundException:java.net.URLClassLoader中的Geoline
$ 1.run(URLClassLoader.java:217)
在java.security.AccessController.doPrivileged(本机方法)
在java。 net.URLClassLoader.findClass(URLClassLoader.java:205)
在java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java: 294)
在java.lang.ClassLoader.loadClass(ClassLoader.java:268)
在java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
找不到主类:Geoline。程序将退出。
输出流:
Geoline类位于/home/geo/Geoline.java 。事情是,无论我在文件系统中的哪个位置,如果我手动执行相同的命令,该类将被执行。当使用 Runtime.getRuntime()。exec 执行二进制文件时,为什么不会发生同样的事情?
编辑:这里是使用verbose标志生成的输出:粘贴链接
编辑:这里是文件的内容:
public class Geoline {
public static void main(String [] args){
System.out.println(blabla);
}
}
很难说出发生了什么,因为你没有发布代码。
然而,这是我最受欢迎的猜测。
您正在尝试将整个命令运行到一个java String ,而java解释器不会将它们识别为不同的参数。
尝试使用 String []
所以而不是
Runtime.getRuntime()。execute (/ usr / bin / java -classpath \/ home / geo\Geoline);
尝试:
Runtime.getRuntime()。execute(new String [] {
/ usr / bin / java, - classpath,/ home / geo,Geoline
});
或者,您可以发布实际代码,我们可以看到发生了什么。
编辑
这是我的尝试。我创建了基本上是Runtime.exec 的 RunIt.java 类,并且 Hello.java 几乎说 Hello + args [0]
这是oupup:
$ cat RunIt.java
import java.io. *;
public class RunIt {
public static void main(String [] args)throws IOException {
进程p = Runtime.getRuntime()exec(args);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = reader.readLine())!= null){
System.out.println(line);
}
reader.close();
}
}
$ cat Hello.java
public class Hello {
public static void main(String [] args){
System.out。 println(Hola:+(args.length> 0?args [0]:a nadie));
}
}
$ javac RunIt.java
$ java RunIt javac Hello.java
$ java RunIt java Hello Mundo
Hola:Mundo
$
I'm executing the java binary from one of my classes, and a ClassNotFoundException gets thrown:
Results of executing:/usr/bin/java -classpath "/home/geo" Geoline
Error stream:
Exception in thread "main" java.lang.NoClassDefFoundError: Geoline
Caused by: java.lang.ClassNotFoundException: Geoline
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: Geoline. Program will exit.
Output stream:
The Geoline class is located at /home/geo/Geoline.java. The thing is, no matter where in the filesystem I'm located, if I execute the same command manually, the class is executed. Why isn't the same thing happening when the binary is executed with Runtime.getRuntime().exec?
edit: here's the output generated with the verbose flag on: pastie link edit: here's the contents of the file :
public class Geoline {
public static void main(String[] args) {
System.out.println("blabla");
}
}
It is quite hard to tell what's going on because you didn't post the code.
However this is my most educated guess.
You're trying to run the whole command into a java String and the java interpreter is not recognizing them as different arguments.
Try packaging them all in a String[]
So instead of
Runtime.getRuntime().execute("/usr/bin/java -classpath \"/home/geo\" Geoline");
Try with:
Runtime.getRuntime().execute( new String[]{
"/usr/bin/java","-classpath","/home/geo","Geoline"
});
Or, you could post your actual code and we can see what's going on.
EDIT
This is my attempt. I create the RunIt.java class which is basically Runtime.exec as I suggested and Hello.java which pretty much says Hello + args[0]
Here's the ouput:
$cat RunIt.java
import java.io.*;
public class RunIt{
public static void main( String [] args ) throws IOException {
Process p = Runtime.getRuntime().exec( args );
BufferedReader reader = new BufferedReader( new InputStreamReader ( p.getInputStream() ) );
String line = null;
while( ( line = reader.readLine() ) != null ) {
System.out.println( line );
}
reader.close();
}
}
$cat Hello.java
public class Hello {
public static void main( String [] args ) {
System.out.println("Hola: " + ( args.length > 0 ? args[0] : " a nadie" ) );
}
}
$javac RunIt.java
$java RunIt javac Hello.java
$java RunIt java Hello Mundo
Hola: Mundo
$
这篇关于我不明白为什么这个ClassNotFoundException被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!