File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a Java Program from Command Prompt" Watch "Running a Java Program from Command Prompt" New topic

Running a Java Program from Command Prompt

Asuthosh Borikar
Ranch Hand

Joined: Sep 29, 2000
Posts: 75
I am a beginner to java and I am facing this problem. I have a program zzz, which has a package statement:
My source file is in c:\com\xxx\yyy folder. I am able to compile the program with the command javac -d c:\ 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 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%;.

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

Joined: Sep 26, 2000
Posts: 4
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:
subject: Running a Java Program from Command Prompt
jQuery in Action, 3rd edition