编译并运行Java应用 [英] Compiling and running Java app

查看:55
本文介绍了编译并运行Java应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译后,我的Java应用无法运行。我的猜测是我的类路径出了问题。

I can't get my java app to run after compiling. My guess is there's something wrong my classpaths.

只是为了清除问题,我使用了IDE,Eclipse,最初是使用它来编译和运行我的应用程序。它运行完美,但是现在我需要严格通过终端/命令行运行它。原因是我遵循一个准则,即如果它不能在命令行上运行,那么它就不存在。 (可以帮助我真正了解我的应用程序)

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)

无论如何,这就是环境

32位Linux AWS发行版(基于CentOS)

32bit Linux AWS distro (Based off of CentOS)


  • yum安装版本*

  • yum安装java *


  • staff / src /com/staffS3/Helpers/Constants.java

  • staff / src / net / UploadFile.java


  • export CLASSPATH = / home / ec2-user / staff / lib / aws- java-sdk-1.2.1.jar:。

  • export 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

正确编译(Atleast w

Compiles correctly (Atleast without any errors)

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 :).

预先感谢

推荐答案


要运行它

To run it

java UploadFile


您不想要 cd 进入 src / net 目录。只需转到 src 并执行

You musn't cd into the src/net directory. Just go to src and do

java net.UploadFile

您需要站在目录中,这样 java 可以找到文件 net / UploadFile.class

You need to stand in a directory such that java can find the file net/UploadFile.class.

名称错误错误这里

还要注意,通过 -cp 设置类路径是首选方法。即,尝试

Note also that setting the classpath through -cp is the preferred way. I.e., try

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






最后,混合.java文件和.class文件不是一个好主意。运行 javac 时,使用 -d 开关指定类文件的目标目录。调用目录 bin build


Finally, mixing .java files and .class files is a bad idea. Use the -d switch when running javac to specify a destination directory for the class files. Call the directory bin or build.

这篇关于编译并运行Java应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆