简体   繁体   中英

Compiling and running Java app

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

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. 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. Use the -d switch when running javac to specify a destination directory for the class files. Call the directory bin or build .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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