my dog learned polymorphism
The moose likes Beginning Java and the fly likes executing java from another directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "executing java from another directory" Watch "executing java from another directory" New topic

executing java from another directory

kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
how to execute java class file resinding in another directory eg;-
if i am executing from c:\test and the class file is in c:\home\agent\test.class
how to give execution coomand for this
c:\test>java test
thanks in advance
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Using the CLASSPATH environment variable. See

[Fixed link - Jim]
[ August 09, 2005: Message edited by: Jim Yingst ]

[Jess in Action][AskingGoodQuestions]
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the responce
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Setting CLASSPATH is problematic if not evil and best avoided. Instead add the required jars for your program to your launch script: java -classpath ...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Use the classpath argument to the JVM or set the environment variable just for one run. Frinstance I have no classpath in my environment by default, and this is a typical batch file for me ...

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 694
Another consideration when setting the classpath is if the class is in a package. Then you need to adjust the path so that it plus the package gives the complete path to the file.
For example if the package is agent and the file is at C:\files\region1\agent\pgm.class
then the classpath should be:
When you execute you need the package as part of the name:
java agent.pgm
I agree. Here's the link:
subject: executing java from another directory
It's not a secret anymore!