?
Solved

Java help for a newbie

Posted on 2006-05-15
30
Medium Priority
?
306 Views
Last Modified: 2013-11-21
Please note that I know nothing about Java so may not be able to answer what may seem to you like a simple question.

I am getting this error:
Exception in thread "main" java.lang.NoClassDefFoundError

My directory is c:\inetpub\wwwroot\java_tests. All my classes and .java files are stored there.

Java Sun Server is stored on c:\Sun\AppServer.

In the cmd line, I have to compile from c:\Sun\AppServer\jdk\bin. Compiling works fine, but trying to run the program using the command java c:\inetpub\wwwroot\java_tests\MyProgram.class gives me the above error.

Please help, it is driving me nuts!!!

Nikki
0
Comment
Question by:nicky2k
  • 12
  • 9
  • 4
  • +2
30 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16682350
>>In the cmd line, I have to compile from c:\Sun\AppServer\jdk\bin

Not good. Add that directory to your environment variables called PATH. Look in Windows help under ENvironment Variables

Try

java c:\inetpub\wwwroot\java_tests\MyProgram
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16682508
>> java.lang.NoClassDefFoundError

The folder that contains the classes also needs to be in the CLASSPATH environment variable. If it does not exist, you can create a system or user environment variable by that name under My Computer -> Properties -> Advanced -> Environment Variables. Put the name of the directory that contains the class you want to run in that variable. You might also want to add the current directory . to it.

http://www.mindprod.com/jgloss/classpath.html
0
 

Author Comment

by:nicky2k
ID: 16682589
Ah!! Office network won't allow me!! Any shortcut method of doing so?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16682609
If you are not an administrative user, you can set it as a user environment variable (not system). You can also set it from the command promot using:

SET CLASSPATH=%CLASSPATH%;whatever directories you want to include sepaarted by semi colon;.

In your case, I guess it should be:

SET CLASSPATH=%CLASSPATH%;c:\inetpub\wwwroot\java_tests;.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16682613
You can also specify a class-path while running a program using:

java -cp c:\inetpub\wwwroot\java_tests MyProgram

Or

java -cp c. MyProgram
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16682616
Sorry the last one was java -cp . MyProgram
0
 

Author Comment

by:nicky2k
ID: 16682628
Still does not work.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16682640
Which options did you try? Is the class in a package? What command did you use to run it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16682649
>>Still does not work.

What error are you getting?
0
 

Author Comment

by:nicky2k
ID: 16683283
The same error.

The class is not in a package.

I tried the set classpath and that seemed fine.

I did notice however that there seems to be a problem with my registry wetings on Windows, which came up during the installation of the jdk 5.0.

0
 
LVL 8

Expert Comment

by:Rytmis
ID: 16684549
Are you doing:

java -cp . MyProgram

-or-

java -cp . MyProgram.class?

You're supposed to use the class name rather than the file name, so the latter variant is wrong.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16687401
Hope JAVA_HOME is pointing to the root directory of your JDK 5 installation.
0
 

Author Comment

by:nicky2k
ID: 16688433
First of all, I don't know what Java_Home is, this is my very first Java program. But the jdk 5 installation is in C:\Program Files\Java.

If it isn't please let me know if I have installed the wrong version, the one I got from Sun's website also installed Net Beans with it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16688446
Well it looks like you have one in c:\Sun\AppServer\jdk\bin too.

Do a full file system search for java.exe and let us know what you find
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16688530
>> I don't know what Java_Home

An environment variable just like the others. Set JAVA_HOME to the root directory of your JDK. Is it C:\Program Files\Java or is it C:\Program Files\Java\jdk1.5.0_06. Also set your PATH env variable to %JAVA_HOME%\bin; followed by whatever was there in the path.
0
 

Author Comment

by:nicky2k
ID: 16689005
I have unistalled the c:\Sun\AppServer\jdk\bin  version and I a, now using the C:\Program Files\Java\jdk1.5.0_06 version. The file called java.exe is located there, but I can not create envoronment variables due to restrictions in the company.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16689030
>>but I can not create envoronment variables due to restrictions in the company.

You create it under the user account, not administrator. See Windows help under 'Environment Variables'
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16689570
Have you tried  mayankeagle's suggestion ?
you don't need to create environment variable :

    java -cp c:\Sun\AppServer\jdk\bin;c:\inetpub\wwwroot\java_tests  MyProgram

You can check the java version you're using :

    java -version

If you cannot modify PATH environment variable, specify java.exe full path :
"C:\Program Files\Java\jdk1.5.0_06\bin\java.exe" -cp c:\Sun\AppServer\jdk\bin;c:\inetpub\wwwroot\java_tests  MyProgram
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16689813
>> but I can not create envoronment variables due to restrictions in the company

I already told you various ways to overcome that (create user environment variable, or use SET CLASSPATH on the command prompt or use the -cp switch to run, etc)
0
 
LVL 8

Expert Comment

by:Rytmis
ID: 16690431
Can you please paste the entire error message you are getting? You've omitted a bit from the end of the message. Here's what I get from the same Sun JVM:

root@beatbox:~# java -cp . ClassName
Exception in thread "main" java.lang.NoClassDefFoundError: ClassName

Your message is missing the bit after NoClassDefFoundError.
0
 

Author Comment

by:nicky2k
ID: 16690913
Ok, I ran the full path in the command line as:
java "C:\Program Files\Java\jre1.5.0_06\bin\java.exe" -cp c:\inetpub\wwwroot
\java_tests  MyProgram

and then I got:
Error: could not open `C:\Program Files\Java\jre1.5.0_06\lib\i386\jvm.cfg'
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16691015
Remove the first word "java"

"C:\Program Files\Java\jre1.5.0_06\bin\java.exe" -cp c:\inetpub\wwwroot\java_tests  MyProgram
0
 

Author Comment

by:nicky2k
ID: 16691096
Now back to the original error. I had a systems admin guy over here change the environment variables for me. Still the same.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16691191
Post the full message.
0
 

Author Comment

by:nicky2k
ID: 16691326
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16691377
Post the output of ECHO %CLASSPATH% and the command you used to run it (from which directory).
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
ID: 16691422
Try changing directory to MyProgram's one :

cd c:\inetpub\wwwroot\java_tests
"C:\Program Files\Java\jre1.5.0_06\bin\java.exe" -cp . MyProgram
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16691442
(also the contents of the folder from where you are running it).
0
 

Author Comment

by:nicky2k
ID: 16691476
Webstorm, I don't know what is different, but it worked. Thanks.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16691521
Perhaps you were not adding it to the class-path correctly. Running it from the current directory solved it. That's why I'd asked for: >> (from which directory)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question