• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Could not find the main class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You run Java classes, not Java class files. Try "java HelloWorldApp" instead.
 
Alec Disharoon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic