编译并运行Java应用程序

发布于 2024-11-14 18:11:11 字数 1921 浏览 2 评论 0原文

问题:

我无法让我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-11-21 18:11:11

运行它

java UploadFile

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

java net.UploadFile

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

“名称错误”错误消息(及其原因)在此处有很好的描述


另请注意,通过 -cp 设置类路径是首选方式。即,尝试

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

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

To run it

java UploadFile

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

java net.UploadFile

You need to stand in a directory such that java can find the file 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. I.e., 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. Use the -d switch when running javac to specify a destination directory for the class files. Call the directory bin or build.

一张白纸 2024-11-21 18:11:11

本质上,您似乎正在尝试使用基本名称运行它,不包括包。

尝试从“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文