aspose file tools*
The moose likes Beginning Java and the fly likes I've got it compiling, now how do I run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Watch "I New topic
Author

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

nik sawtschuk
Greenhorn

Joined: Aug 18, 2010
Posts: 5
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
nik sawtschuk
Greenhorn

Joined: Aug 18, 2010
Posts: 5
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
nik sawtschuk
Greenhorn

Joined: Aug 18, 2010
Posts: 5
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

1. Which folder is your class in ?
2. Is there a CLASSPATH variable under System ?
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

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.


MCSA 2003 | Preparing For OCPJP/SCJP6
nik sawtschuk
Greenhorn

Joined: Aug 18, 2010
Posts: 5
Thanks for the help guys!

I just followed Vishal's 1 and 2 and it worked right away
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Why did you have a system CLASSPATH set in the first place? That is usually a mistake.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

And moreover, it's JAVA_HOME
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 18, 2010
Posts: 5
I never had a SYSTEM CLASSPATH, just a user one.

But I removed that anyways now.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I've got it compiling, now how do I run?