aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with -cp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with -cp" Watch "problem with -cp" New topic
Author

problem with -cp

jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
Hi friends,
I'm preparing the text book by K&B for SCJP 5.0 & quite new to this forum.
I have a problem in -cp in lesson 10 as per the book. I'm using WindowsXP & jdk-1_5_0.

Scenario:
My java file to be compiled is on C:\ls10\A\A1\Test.java. But iwant it compiling & running from C:\ls10. I attempted as

C:ls10>javac -cp A\A1 Test.java (doesn�t work)
C:ls10>javac -cp .;A\A1 Test.java (doesn�t work)

but once i compiled with out using class path by
C:ls10>javac A\A1\Test.java (compilation succeeds)
then, the -cp works to run the Test.class file
C:ls10>java -cp A\A1 Test(works & i get the output)

Can some one please tell why -cp doesn�t work for compilation but works for running the program even when i attempted from the same directory by using the same directory structure with -cp for both 'javac' & 'java' ?

Thanks in advance.
jagan.k
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

By default javac looks in to current directory
but you can provide target path for javac that is relative to current directory
java loking for class file to execute
-cp option override default classpath for java and search in the given -cp path

Hope this may help you

Regards
Ninad


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
Thanks Ninad Kulkarni for finding time to respond me. I've worked out with all possible ways but still sticking with the same problem.

My question is WHEN THE -cp WORKS FINE FOR java WHY DOES'T WORK FOR javac EVEN WITH THE SAME CLASS PATH I'VE GIVEN WITH.

Can you please tell me the command with -cp for 'javac' for my case if possible?

Thanks once again.
jagan.k
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
"java -cp A\A1 Test" should NOT work!

The class file will be in the same directory as the source. Use package notation :
"java A.A1.Test"

use classpath when running from a directory other that the package "root" (K&B might help u) e.g.
"java ..\.. A.A1.Test" when running in c:\ls10\A\A1


javac just works with files, the target must have a path if your working directory is somewhere else (you class doesn't need a classpath) :
"javac A\A1\Test.java" (on windows)
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
exam tip: remember that java/javac will also look in <installed java>/jre/lib/ext for any jars without explicitly naming that directory in the classpath

jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
Thank you very much Frank Zito.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with -cp