Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Could not find the main class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!