Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Can't execute from command line

Thread view: 
James - 25 Jan 2006 21:01 GMT
Hi

I've been using BlueJ to learn Java and can build classes with a main method
and execute them succesfully in this environment.

However, I'm starting to use the command line in Windows XP and am meeting a
problem...

Having changed the path variable to point to jdk1.5.0_06 I can now use the
javac and java commands, but I am receiving an error on the command line
when I try to execute my compiled class file. Here's what I have:-

Test.java:

public class Test
{
   public static void main(String[] args)
   {
       System.out.println("Hello World");
   }
}

>javac Test.java produces no errors...

>java Test produces this error:-

Exception in thread "main" java.lang.NoClassDefFoundError: Test

What am I doing wrong??
jcsnippets.atspace.com - 25 Jan 2006 21:05 GMT
> Hi
>
[quoted text clipped - 25 lines]
>
> What am I doing wrong??

The first thing that comes to mind is the classpath variable. If this is not
set to include the current working directory, your class (Test) cannot be
found, and therefor you will not be able to run your program.

Try this:
java -cp . Test
instead of
java Test

-cp sets the classpath, and . is your current working directory.

Best regards,

JC
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks
James - 25 Jan 2006 21:19 GMT
>> Hi
>>
[quoted text clipped - 48 lines]
> http://jcsnippets.atspace.com
> a collection of source code, tips and tricks

Thankyou, that does work. However, as I am executing these commands from the
directory containing the Test files, how come "javac Test.java" works, but
"java Test" doesn't?
Roedy Green - 26 Jan 2006 00:38 GMT
>, how come "javac Test.java" works, but
>"java Test" doesn't?
because javac is looking for the unqualified name of a source file
and java.exe is looking for the a fully qualified class name.

Javac does not look on the classpath.  Java.exe does.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

jcsnippets.atspace.com - 26 Jan 2006 19:38 GMT
> Thankyou, that does work. However, as I am executing these commands from the
> directory containing the Test files, how come "javac Test.java" works, but
> "java Test" doesn't?

Because they are looking for different things.

javac.exe looks for files to compiler, whereas java.exe looks for classes
which it can execute. And to find those classes, java.exe looks at the
classpath.

Regards,

JC
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks
James - 27 Jan 2006 09:18 GMT
>> Thankyou, that does work. However, as I am executing these commands from
> the
[quoted text clipped - 7 lines]
> which it can execute. And to find those classes, java.exe looks at the
> classpath.

It just seems strange, and annoying, that java.exe doesn't look for those
classes in the directory it is being executed in. The Win XP command line
works by looking in the current directory AND the path variable, so why
can't java.exe follow the same standard? Ah well, gripe over...
jcsnippets.atspace.com - 29 Jan 2006 00:34 GMT
> >> Thankyou, that does work. However, as I am executing these commands from
> > the
[quoted text clipped - 12 lines]
> works by looking in the current directory AND the path variable, so why
> can't java.exe follow the same standard? Ah well, gripe over...

Hi James,

Well, it may seem strange at first, and even annoying, but the fact of the
matter is that there really is a difference. And this difference explains
the behaviour you are experiencing now.

Files and directories are different concepts than classes and packages.

You mentioned yourself that the Win XP command line uses the current
directory and the path variable, well, the classpath variable for java.exe
can be compared to the path variable for the Win XP cli.

But the Win XP cli only works for Win XP, while Java (the language) is
intended to be run on *any* system (that is, any system for which a JVM is
available).

Anyway, there is a simple way to solve all this: just like you can adjust
the path variable, you can adjust the classpath variable. I'm on Win2k at
the moment, but I suspect XP to be about the same - look in settings/control
panel/system, then click the advanced tab, then the environment variables
button, and look for CLASSPATH under system variables.

Click edit, and add ;. to the classpath variable. Then click ok, etc... to
save this setting.

From now on, you'll be able to use java SomeClass without having to use -cp
. in the command line.

Regards,

JC

ps: I hope this post made some sense, I feel like falling asleep any minute
now :)
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks
James - 25 Jan 2006 21:37 GMT
Just googled and found http://www.kevinboone.com/classpath.html which seems
to explain the maddening but no doubt logical situation.

Many thanks for your prompt help.

Regards
James
Steve W. Jackson - 26 Jan 2006 15:44 GMT
> Just googled and found http://www.kevinboone.com/classpath.html which seems
> to explain the maddening but no doubt logical situation.
[quoted text clipped - 3 lines]
> Regards
> James

It may explain it, but it also proceeds to approve of using the
CLASSPATH variable.  Sun rightly advises NOT to use the system-wide
variable.
Signature

Steve W. Jackson
Montgomery, Alabama

Roedy Green - 26 Jan 2006 00:37 GMT
>NoClassDefFoundError

see http://mindprod.com/jgloss/caq.html
http://mindprod.com/jgloss/gettingstarted.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.