[英]java.lang.NoClassDefFoundError when using console
I have a problem with the execution of programs by console. 我在通过控制台执行程序时遇到问题。 It always throws java.lang.NoClassDefFoundError but if i run the same application with netbeans it works perfectly.What can i do?
它总是抛出java.lang.NoClassDefFoundError,但是如果我使用netbeans运行相同的应用程序,它将运行完美。我该怎么办?
You can add classpath in Java by providing option "-cp" or "-classpath" while running Java program. 您可以通过在运行Java程序时提供选项“ -cp”或“ -classpath”来在Java中添加类路径。
set classpath there as shown below : 设置classpath如下所示:
Set CLASSPATH=.;/com/test/classes
java -cp $CLASSPATH Test
By default Java CLASSPATH points to current directory denoted by "." 默认情况下,Java CLASSPATH指向由“。”表示的当前目录。 and it will look for any class only in current directory.
并且它将仅在当前目录中查找任何类。
Your classpath is set differently ie jar(s) on the classpath in NetBeans are not on the classpath when you run the same code from the console. 您的类路径设置不同,即当您从控制台运行相同代码时,NetBeans中类路径上的jar不在类路径上。
A quick code snippet which outputs what is on your classpath: 一个快速的代码片段,输出您的类路径中的内容:
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){ System.out.println(url.getFile()); }
From the console, use the -cp option to add the necessary jars. 在控制台中,使用-cp选项添加必要的jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.