This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help getting javac/java commands working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help getting javac/java commands working" Watch "Help getting javac/java commands working" New topic
Author

Help getting javac/java commands working

Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

Hi All,

I've been programming and in school for it for quite awhile, but have never ran code from a terminal.

I added the environment variable which points to the JRE \bin directory. The javac and java commands are recognized. My problem is in running the .class file. When doing so, I receive this terrifying error:

C:\Users\troberts\Documents\Java Projects>java ProjectThree
Exception in thread "main" java.lang.NoClassDefFoundError: ProjectThree (wrong n
ame: projectthree/ProjectThree)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

I've done some google research and found that some additional environment variables may need to be added... but when I did that, the result doesn't change. There were some additional comments about adding the dot opr at the end, but that didn't seem to work for me either.

Please help.. this is an annoying problem for me to have.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

What is the name of your Java file? ProjectThree.java or projectthree.java
What is the package name and what is the name of the folder?

WP
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

It seems that you did not use the fully qualified name of that class. Do you have the ProjectThree java file in a package called projectthree? If so, you should use the package name when trying to run your file. See here, under "Setting up the CLASSPATH" to understand how this works:
http://www.jarticles.com/package/package_eng.html
Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

Hello again,

I am not sure where projectthree is coming from.

My project sits in a folder called Java Projects and it contains:

Directory of c:\Users\troberts\Documents\Java Projects

05/02/2012 02:38 PM <DIR> .
05/02/2012 02:38 PM <DIR> ..
05/02/2012 12:32 PM 472 ProjectOne.class
05/02/2012 12:42 PM 249 ProjectOne.java
05/02/2012 02:26 PM 443 ProjectThree.class
05/02/2012 02:25 PM 322 ProjectThree.java
05/02/2012 12:56 PM 418 ProjectTwo.class
05/02/2012 12:55 PM 347 ProjectTwo.java
05/02/2012 02:39 PM <DIR> Test
6 File(s) 2,251 bytes
3 Dir(s) 194,949,726,208 bytes free

projectthree apparently exists only in the terminals mind. When I run java ProjectThree, I get that error. Same with the other projects...

Thanks for that link. I learned something reading about the fully qualified names and using the dot operator there, but that's not the issue I'm having; this is only a hello world program.

Whats interesting is I got it to work running it from the root of my documents. Does the IDE do anything that can mess this up?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

Travis Roberts wrote:Hello again,

I am not sure where projectthree is coming from.

My project sits in a folder called Java Projects and it contains:

Directory of c:\Users\troberts\Documents\Java Projects

05/02/2012 02:38 PM <DIR> .
05/02/2012 02:38 PM <DIR> ..
05/02/2012 12:32 PM 472 ProjectOne.class
05/02/2012 12:42 PM 249 ProjectOne.java
05/02/2012 02:26 PM 443 ProjectThree.class
05/02/2012 02:25 PM 322 ProjectThree.java
05/02/2012 12:56 PM 418 ProjectTwo.class
05/02/2012 12:55 PM 347 ProjectTwo.java
05/02/2012 02:39 PM <DIR> Test
6 File(s) 2,251 bytes
3 Dir(s) 194,949,726,208 bytes free

projectthree apparently exists only in the terminals mind. When I run java ProjectThree, I get that error. Same with the other projects...

Thanks for that link. I learned something reading about the fully qualified names and using the dot operator there, but that's not the issue I'm having; this is only a hello world program.

Whats interesting is I got it to work running it from the root of my documents. Does the IDE do anything that can mess this up?



"projectthree" is the package name -- take a look at your source to see if you defined the class in a package. An IDE takes care of everything for you in this regard -- if you define a class in a package, it will automatically put it into a directory structure for you. Without an IDE, it is your responsibility to keep these in sync.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

I'm still lost. I can be stubbornly dim-witted sometimes.

The problem is isolated to just code in packages.

Fortunately, I've unearthed my Sybex SCJP book and it discusses this at length. I will give it a good read.

Thanks to all for the guidance.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Henry Wong wrote:"projectthree" is the package name -- take a look at your source to see if you defined the class in a package.

Did you check the source code for package declaration as Henry suggested. Can you show us the code?
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Travis Roberts wrote:Hello again,

I am not sure where projectthree is coming from.

My project sits in a folder called Java Projects and it contains:

Directory of c:\Users\troberts\Documents\Java Projects

05/02/2012 02:38 PM <DIR> .
05/02/2012 02:38 PM <DIR> ..
05/02/2012 12:32 PM 472 ProjectOne.class
05/02/2012 12:42 PM 249 ProjectOne.java
05/02/2012 02:26 PM 443 ProjectThree.class
05/02/2012 02:25 PM 322 ProjectThree.java
05/02/2012 12:56 PM 418 ProjectTwo.class
05/02/2012 12:55 PM 347 ProjectTwo.java
05/02/2012 02:39 PM <DIR> Test
6 File(s) 2,251 bytes
3 Dir(s) 194,949,726,208 bytes free

projectthree apparently exists only in the terminals mind. When I run java ProjectThree, I get that error. Same with the other projects...

Thanks for that link. I learned something reading about the fully qualified names and using the dot operator there, but that's not the issue I'm having; this is only a hello world program.

Whats interesting is I got it to work running it from the root of my documents. Does the IDE do anything that can mess this up?


You are running this from the IDE then? If you are, instead go to c:\Users\troberts\Documents\Java Projects and do the following:

c:\Users\troberts\Documents\Java Projects>java ProjectThree

... if that does not work, try this:

c:\Users\troberts\Documents\Java Projects>java -cp . ProjectThree

If that still does not work, as mentioned, please post the ProjectThree.java file.

If though it does work, then tell us how you are executing the file from within the IDE.


~Bill
Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

Here is the code:



When I go all the way to the .class file

C:\Users\troberts\Documents\Java Projects\ProjectThree\build\classes\projectthree>java ProjectThree

I get:

Error: Could not find or load main class ProjectThree

C:\Users\troberts\Documents\Java Projects\ProjectThree\build\classes\projectthree>java projectthree.ProjectThree
Error: Could not find or load main class projectthree.ProjectThree

C:\Users\troberts\Documents\Java Projects\ProjectThree\build\classes\projectthree>java projectthree\ProjectThree
Error: Could not find or load main class projectthree\ProjectThree


These results are the same when I use CLASSPATH=C:\Users\troberts\Documents\Java Projects\ProjectThree

The command with the -cp options doesn't seem to work either.

Man... I'm having a harder time with Java than I did with Assembler so far. I can't even get Java to run!



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
Are you sure that classpath is correct? I thought that you used DocumentsAndSettings or Documents%20And%20Settings on Windows®, rather than users. Also check the spelling of project three. You have capital letters in what you quoted, but the package name is (as it ought to be) in lower-case throughout.
Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

The path is correct.

The lower case is the package. The second ProjectThree is the .class file I am trying to make run.

Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Travis Roberts wrote:The path is correct.

The lower case is the package. The second ProjectThree is the .class file I am trying to make run.



Then it is just as Henry suggested above. back up to:

C:\Users\troberts\Documents\Java Projects\ProjectThree\build\classes

and run as this:

C:\Users\troberts\Documents\Java Projects\ProjectThree\build\classes>java -cp . projectthree.ProjectThree
Travis Roberts
Greenhorn

Joined: Jul 13, 2009
Posts: 10

This works at home!

The issue is probably with my system at work or perhaps the java install.

Thanks for all the help offered. Onward to the Cattle Drive, then.
 
 
subject: Help getting javac/java commands working
 
Similar Threads
Issues with cmd java command
gettin error while running program
Cannot figure out what I forgot to set, or I set wrong, or forgot to install?
Runtine Exception UnsupportedClassVersionError
UnsupportedClassVersionError (unsupported major.minor version 50.0)