BrianJakovich
BrianJakovich

Reputation: 1614

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

The Application structure

Classpaths

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

Upvotes: 1

Views: 2214

Answers (2)

aioobe
aioobe

Reputation: 421130

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.

Upvotes: 4

user7094
user7094

Reputation:

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

Upvotes: 2

Related Questions