This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Running a Java Program from Command Prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a Java Program from Command Prompt" Watch "Running a Java Program from Command Prompt" New topic
Author

Running a Java Program from Command Prompt

Asuthosh Borikar
Ranch Hand

Joined: Sep 29, 2000
Posts: 75
Hi,
I am a beginner to java and I am facing this problem. I have a program zzz, which has a package statement: com.xxx.yyy.
My source file is in c:\com\xxx\yyy folder. I am able to compile the program with the command javac -d c:\ zzz.java and the .class file is placed in c:\com\xxx\yyy, but I get a NoClassDefFoundError when I try : java zzz, from c:\com\xxx\yyy. If I give the fully qualified name java com.xxx.yyy.zzz from c:\, it executes successfully. Is it a must that I give the fully qualified name each time I try to execute a program in a package? If yes, why do we have to include the root directory of the package in the CLASSPATH? Only so that the compiler can find the classes when they are imported in other classes? Please help me.
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
I guess you can solve the problem by adding the current directory to the classpath .
classpath = %classpath%;.


Regards,<BR>Shree
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The current directory is always in the default class path. When u dont specify the path of your class file it tries to see if it is there in the current directory or in the class path.
As u have compiled giving the fully qualified name, i suggest u go into the folder where ur programs are and compile/run it, or put the working folder in your class path to compile or run from anywhere.
Srinivasa Gajula
Greenhorn

Joined: Sep 26, 2000
Posts: 4
Asuthosh,
A easy way is to create a batch file that sets up path for all your class files.
Add the following to batch file(say setpath.bat)
SET CLASSPATH=.;%CLASSPATH%;c:\com\xxx\yyy
run this batch file in a dos session and then run your java program
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a Java Program from Command Prompt
 
Similar Threads
Doubt in ch-10(Kathhy Sierra)
java.lang.NoClassDefFoundError
Invoking "java" in WEB-INF/classes
Invalid character in input error when using javadoc
I'am need help on page.no 82 HFS