Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I've got it compiling, now how do I run?

 
nik sawtschuk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How's THAT for a descriptive title?

Java noob here,
So it took me some time, but I managed to compile this simple program, and make a .class file.

When I try to type 'java app' right after, it tells me this:
Exception in thread "main" java.lang.NoClassDefFoundError: app
Caused by: java.lang.ClassNotFoundException: app
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: app. Program will exit.

Any ideas?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, and congratulations on compiling your file!

It looks like Java cannot find your class file. This is probably because you have set a system CLASSPATH variable and did not include a dot (.) for the current directory. You can test this by including a classpath of the current directory (.) in your command to run the program:

java -cp . app

If this works, then that's the problem, and you should consider removing your system CLASSPATH entirely (if you do not need if for some other purpose) or at least adding a dot to the list.
 
nik sawtschuk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh okay putting my directory where the class file is located where the dot is in the line you provided works!

thanks alot, but how can I make it so I can just type 'java whatever' if i want to run the program (I'll put all my class files in the same folder)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nik sawtschuk wrote:but how can I make it so I can just type 'java whatever' if i want to run the program (I'll put all my class files in the same folder)

Add your directory to the CLASSPATH environment variable.
 
nik sawtschuk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried this:

System, click on Environment Variables, under User I made "CLASSPATH" and assigned it my directory. 'java app' still doesn't work after this has been done.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Which folder is your class in ?
2. Is there a CLASSPATH variable under System ?
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear nik,

two things you have to do :
1] Set CLASSPATH properly in your environment variable, for your class directory.
2] Add <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib in PATH environment variable.
or
3] Set JAVAHOME environment variable for <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib.

*Separate two fields by a semicolon*

then, logout and login again or restart your system. and try to compile at this time, surely works.
 
nik sawtschuk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys!

I just followed Vishal's 1 and 2 and it worked right away
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you have a system CLASSPATH set in the first place? That is usually a mistake.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Kashyap wrote: . . .
1] Set CLASSPATH properly in your environment variable, for your class directory.
2] Add <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib in PATH environment variable.
or
3] Set JAVAHOME environment variable for <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib.
. . .
Disagree. If he is getting that sort of error his PATH is already correct. It is usually a mistake to set a system CLASSPATH at all. If there already is a CLASSPATH set, you only need to add .; or ;. or ;.; to it. If I remember correctly, the JAVAHOME variables should not include the word "bin".
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And moreover, it's JAVA_HOME
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Kashyap wrote:1] Set CLASSPATH properly in your environment variable, for your class directory.
2] Add <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib in PATH environment variable.
or
3] Set JAVAHOME environment variable for <Drive>:\Program Files\Java\Jdk<version>\bin and <Drive>:\Program Files\Java\Jdk<version>\lib.

Step 2: It is not necessary to add the lib directory of the JDK to the PATH. The PATH is only for the operating system to find executable files. The lib directory does not contain executable (*.exe) files.

Step 3: Setting JAVAHOME (or JAVA_HOME) is not necessary for Java itself. Some external programs (for example Apache Tomcat) expect JAVA_HOME to be set, but it should be set to the JDK directory itself, and not to the bin and/or lib directories of the JDK.

As Campbell says, it is better to not set CLASSPATH at all. If you don't set CLASSPATH, Java will look in the current directory by default.
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN MY OPINION

You all are correct at your own place.

(We are not there with Nik.)
But as per Nik's second question I have written all possibilities for the requirement.
Because, you all had written a way like to understand for any professional programmer but not for a novice. That's why; Nik faced problem to solve it out. That's why; i have written all possibilities regarding requirement. And it really worked(Nik said)

And thanks for guiding me and providing information in such a good way.
Be always with me as a guiding light.
Thanking you all.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again...
marc weber wrote:...

java -cp . app

If this works, then that's the problem, and you should consider removing your system CLASSPATH entirely (if you do not need if for some other purpose) or at least adding a dot to the list.

So... Since this worked, that's the problem, and you should consider removing your system CLASSPATH entirely.
 
nik sawtschuk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never had a SYSTEM CLASSPATH, just a user one.

But I removed that anyways now.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nik sawtschuk wrote:I never had a SYSTEM CLASSPATH, just a user one.

But I removed that anyways now.

Okay, that will do it too.

(I see I wasn't clear. I said "system" to mean elsewhere in your environment, as opposed to passing an ad hoc classpath with the java command.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:. . . I said "system" to mean elsewhere in your environment, as opposed to passing an ad hoc classpath with the java command.
So did I.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic