编译并运行Java应用程序
问题:
我无法让我的java应用程序在编译后运行。我的猜测是我的类路径有问题。
为了澄清问题,我使用了 IDE(Eclipse),我最初使用它来编译和运行我的应用程序。它运行完美,但现在我需要严格通过终端/命令行运行它。原因是我遵循一个准则,如果它不能在命令行上运行,那么它就不存在。 (帮助我真正了解我的应用程序)
无论如何,这是环境
操作系统
32位Linux AWS发行版(基于CentOS)
安装的软件
- yum install subversion*
- yum install java*
< strong>应用程序结构
- staff/src/com/staffS3/Helpers/Constants.javastaff
- /src/net/UploadFile.java
类路径
- 导出CLASSPATH =“/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar:”
- 导出JAVA_HOME =“/ usr / lib / jvm / jre /”
编译
javac /home/ec2-user/staff/src/com/staffS3/Helpers/Constants.java /home/ec2-user/staff/src/net/UploadFile.java
正确编译(至少没有任何错误)
运行它
java UploadFile
错误
Exception in thread "main" java.lang.NoClassDefFoundError: UploadFile (wrong name: net/UploadFile)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: UploadFile. Program will exit.
去过这有一段时间了。希望有人会注意到我错过的那个小(或大)东西:)。
提前致谢
The problem:
I can't get my java app to run after compiling. My guess is there's something wrong my classpaths.
Just to clear things up I have used an IDE, Eclipse, and I originally used that to compile and run my application. It ran perfectly, but now I need to run it strictly through the terminal/command line. The reason is that I follow a guideline that if it cant be run on the command line then it doesn't exist. (Helps me really get to know my applications)
Anyway here's the environment
OS
32bit Linux AWS distro (Based off of CentOS)
Installed software
- yum install subversion*
- yum install java*
The Application structure
- staff/src/com/staffS3/Helpers/Constants.java
- staff/src/net/UploadFile.java
Classpaths
- export CLASSPATH="/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar:."
- export JAVA_HOME="/usr/lib/jvm/jre/"
Compiling
javac /home/ec2-user/staff/src/com/staffS3/Helpers/Constants.java /home/ec2-user/staff/src/net/UploadFile.java
Compiles correctly (Atleast without any errors)
To run it
java UploadFile
Error
Exception in thread "main" java.lang.NoClassDefFoundError: UploadFile (wrong name: net/UploadFile)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: UploadFile. Program will exit.
Been at this for a while. Hopefully someone will notice that little(or huge) thing I'm missing :).
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能
cd
进入src/net
目录。只需转到src
并执行您需要站在一个目录中,以便
java
可以找到文件net/UploadFile.class
。“名称错误”错误消息(及其原因)在此处有很好的描述。
另请注意,通过
-cp
设置类路径是首选方式。即,尝试最后,混合 .java 文件和 .class 文件是一个坏主意。运行
javac
时使用-d
开关指定类文件的目标目录。调用目录bin
或build
。You musn't
cd
into thesrc/net
directory. Just go tosrc
and doYou need to stand in a directory such that
java
can find the filenet/UploadFile.class
.The "Wrong Name" error message (and it's cause) is described well here.
Note also that setting the classpath through
-cp
is the preferred way. I.e., tryFinally, mixing .java files and .class files is a bad idea. Use the
-d
switch when runningjavac
to specify a destination directory for the class files. Call the directorybin
orbuild
.本质上,您似乎正在尝试使用基本名称运行它,不包括包。
尝试从“src”文件夹运行它,然后运行
java net.UploadFile
Essentially it looks like you're trying to run it using the base name, not including the package.
Try running it from the "src" folder, and running
java net.UploadFile