• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with -cp

 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Frank Zito.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic