Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

SCJP chapter 10 : Development

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

There is one question in self-test of K&B book which I would like to discuss with you.

The question is :

Given the following directory structure:

test - |
|- GetJar.java
|
|- myApp - |
|-Foo.java

And given the contents of GetJar.java and Foo.java





If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar located in the test, which set(s) of command will compile GetJar.java and produce the output 8?

And correct answer given in the book is :

javac -classpath MyJar.jar GetJar.java
java GetJar


Now , as per my understanding java compiler won't search for files in current directory if it is not specified in classpath with (.). Then if we are not specifying (.) in classpath, how compiler is able to find GetJar.java?


This may sound like a silly question bu t as I am using IDE a lot for development and I have never used command prompt for compiling and executing java files, I am confused.

Could you please help me out?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-classpath is used to specify the path where the compiler will look for .class files required to compile the passed .java file. Since in the command only GetJar.java is specified then the compiler will look for GetJar.java in the current directory. The command is equivalent to

javac -classpath MyJar.jar .\GetJar.java

Even you can compile a file from a different directory by passing an absolute file path

javac -classpath MyJar.jar C:\temp\GetJar.java
 
Swapnil Suryakant Prabhavalkar
Greenhorn
Posts: 23
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Thanks.

But in the next example in K&B, they say that specifying (.) in classpath is necessary while giving java command from same directory if .class file is in same directory?

Does javac and java behave differently with this (.) thing?
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac command requires you to pass it a .java file name which it would compile. If you provide a relative file name then javac will automatically relate it with the current directory as would any other command which requires a file name (for example copy command or del command of windows). java command is different. java command by default looks for classes in the current directory but if you specify the classpath then it will only look for classes in the directories specified in the classpath...
 
Swapnil Suryakant Prabhavalkar
Greenhorn
Posts: 23
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it

Thanks a lot, Ankit.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Swapnil's question,I want to know why
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar.java
is incorrect?

Only one thing is specified in book is current directory i.e. should be super directory of root dirctry i.e myApp.Difference for javac and java is not specified
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Go through the following links. It helps me to to get OCJP Certified.

SCJP 6(OCJP) Mock

scjp(OCJP)5 Mock
Core Java
Java EE 5 Business Component Developer Certified Professional Upgrade
 
reply
    Bookmark Topic Watch Topic
  • New Topic