aspose file tools*
The moose likes Beginning Java and the fly likes Could not find the main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could not find the main class" Watch "Could not find the main class" New topic
Author

Could not find the main class

Alec Disharoon
Greenhorn

Joined: Apr 25, 2010
Posts: 3
Hello everyone. I'm trying to run my first Java program in Windows. I've previously had this working in Linux.

I'm running Windows 7 64 bit. I have the following installed:

- Java(TM) 6 Update 20 (64-bit)
- Java(TM) SE Development Kit 6 Update 19 (64-bit)

I have the following environment variables set:

JAVA_HOME - C:\Program Files\Java\jdk.1.6.0_19
Path - %JAVA_HOME%\bin

On my desktop I have a file called HelloWorldApp.java, which I was able to compile on the command prompt with 'javac HelloWorldApp.java'. This produced a file called HelloWorldApp.class on my desktop. I then ran 'java HelloWorldApp.class' and got:

Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\Alec\Desktop\HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: C:\Users\Alec\Desktop\HelloWorldApp.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: C:\Users\Alec\Desktop\HelloWorldApp.class. Program will exit.

Different attempts(running this from the same directory as the java binary, for instance) have produced the same thing.

Any ideas?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You run Java classes, not Java class files. Try "java HelloWorldApp" instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alec Disharoon
Greenhorn

Joined: Apr 25, 2010
Posts: 3
How am I to run the class without specifying where the class can be found? Is it not smart enough to include the file I am providing as an argument in the classpath? I'm coming from a python background here. Should I just make root (C:\) the classpath so I don't have to deal with this?
Alec Disharoon
Greenhorn

Joined: Apr 25, 2010
Posts: 3
Ah - I figured it out. When I tried this like you recommended I tabbed for autocompletion in the terminal and got 'java .\HelloWorldApp' which obviously does not exist. Wasn't thinking.

I'm not sure I understand the mechanism by which classes are searched for, but this works for now.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

The class path should include the location for the class files. Keep in mind that this includes the package structure; if you have a Java class called "HelloWorldApp" that is in package "hello", then you should store this file as "<class path part>\hello\HelloWorldApp.class", and run it as "java hello.HelloWorldApp". In your case, the class path should include "." (the current directory) if you run "java HelloWorldApp" from your desktop, or "C:\Alec\Desktop" otherwise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could not find the main class