• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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)
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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 VI Editor Ubuntu
  • 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 ?
 
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Marshal
Posts: 70228
282
  • 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
Marshal
Posts: 70228
282
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And moreover, it's JAVA_HOME
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
Mac Safari Java
  • 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
Mac Safari Java
  • 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
Marshal
Posts: 70228
282
  • 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.
 
Hey cool! They got a blimp! But I have a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic