简体   繁体   English

编译运行Java app

[英]Compiling and running Java app

The problem:问题:

I can't get my java app to run after compiling.编译后无法运行我的 java 应用程序。 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.为了澄清问题,我使用了 IDE、Eclipse,我最初使用它来编译和运行我的应用程序。 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) 32 位 Linux AWS 发行版(基于 CentOS)

Installed software安装的软件

  • yum install subversion*百胜安装颠覆*
  • yum install java*百胜安装java*

The Application structure应用结构

  • staff/src/com/staffS3/Helpers/Constants.java员工/src/com/staffS3/Helpers/Constants.java
  • staff/src/net/UploadFile.java员工/src/net/UploadFile.java

Classpaths类路径

  • export CLASSPATH="/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar:."导出 CLASSPATH="/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar:."
  • export JAVA_HOME="/usr/lib/jvm/jre/"导出 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提前致谢

To run it运行它

java UploadFile

You musn't cd into the src/net directory.你不能cd进入src/net目录。 Just go to src and do只需 go 到src并执行

java net.UploadFile

You need to stand in a directory such that java can find the file net/UploadFile.class .您需要站在一个目录中,以便java可以找到文件net/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.另请注意,通过-cp设置类路径是首选方式。 Ie, try即,尝试

java -cp .:/home/ec2-user/staff/lib/aws-java-sdk-1.2.1.jar net.UploadFile

Finally, mixing.java files and.class files is a bad idea.最后,混合.java 文件和.class 文件是个坏主意。 Use the -d switch when running javac to specify a destination directory for the class files.运行javac时使用-d开关指定 class 文件的目标目录。 Call the directory bin or build .调用目录binbuild

Essentially it looks like you're trying to run it using the base name, not including the package.本质上,您似乎正在尝试使用基本名称运行它,不包括 package。

Try running it from the "src" folder, and running java net.UploadFile尝试从“src”文件夹运行它,然后运行java net.UploadFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM