编译基本 Hello World 程序时抛出 ClassNotFoundException
我只是想编译并运行一个非常简单的测试程序,但它根本不起作用,而且我不知道问题是什么。
我有一个java项目,一直压在我身上,而我对java知之甚少,甚至一无所知。特别是从 Windows 命令行编译。
我有两个 Jars,我需要用它们来编译一个简单的“hello world”程序。
这是我的“build.bat”
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
然后,我:
C:\jdk1.6.0_21\bin\java sample
这会返回错误:
线程“main”中出现异常java.lang.NoClassDefFoundError:示例 造成原因: java.lang.ClassNotFoundException: 样本 在 java.net.URLClassLoader$1.run(URLClassLoader.java:202) 在java.security.AccessController.doPrivileged(本机 方法 在 java.net.URLClassLoader.findClass(URLClassLoader.java:190 在 java.lang.ClassLoader.loadClass(ClassLoader.java:307) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java: 在 java.lang.ClassLoader.loadClass(ClassLoader.java:248)
无论我如何设置我的类路径,我一生都无法运行这个 HELLO WORLD 程序。
有人可以帮我吗?我正在拔头发。
I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.
I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.
I have two Jars that I need to compile a simple "hello world" program with.
Here's my "build.bat"
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
Then, I:
C:\jdk1.6.0_21\bin\java sample
This spits back the error:
Exception in thread "main" java.lang.NoClassDefFoundError: sample
Caused by:
java.lang.ClassNotFoundException:
sample
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.
Can someone please help me out? I'm pulling my hair out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以指定解释器的类路径来定位您的类:
这将从任何工作目录运行您的类。
You can also specify the classpath for the interpreter to locate your class:
This would run your class from any working directory.
从包含类的目录运行程序:
C:\Users\FREYERA\Desktop\Test\>java example
CLASSPATH(通常)包含当前目录。
如果当前目录中有“sample.class”,并且还需要 test1.jar 和 test2.jar 中的类,则这应该有效:
java -cp“test1.jar;test2.jar;”。示例
Run your program from the directory with the class in it:
C:\Users\FREYERA\Desktop\Test\>java sample
CLASSPATH (normally) includes the current directory.
If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:
java -cp "test1.jar;test2.jar;." sample
运行此命令后
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C: \Users\FREYERA\Desktop\Test\test2.jar"; “C:\Users\FREYERA\Desktop\Test\sample.java”
它将在此位置“C:\Users\FREYERA\Desktop\Test\”创建一个 .class 文件。即sample.class
您需要转到此文件夹位置并运行java 命令来执行该程序。确保您的“JAVA_HOME”环境变量已设置。
或者您可以将sample.class文件复制到“C:\jdk1.6.0_21\bin\”文件夹并运行命令。
Afer running this command
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class
You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.
Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.