Compiling and running Java app

倖福魔咒の 提交于 2020-01-05 07:27:16

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6279190/compiling-and-running-java-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!