Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

-classpath questions - confused

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why I am getting so confused about this, but help would be appreciated. I have the following example:

Given 2 files:



And the following directory stucture:



If the current directory is test and the file pkg/Kit.class is in KitJar.jar, which command line will produce the output: bc? (choose all that apply)


a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jat:. UseKit a b c



Can someone please explain to me carefully which answers above are correct/incorrect, and exactly why? My brain is just freezing on this particular topic!!!


 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you try our the program/commands? Running each answer option should give you better understanding what the program is doing.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

K. Tsang wrote:Have you try our the program/commands? Running each answer option should give you better understanding what the program is doing.



I am working in a windows environment, not UNIX, so I don't think I can try them unfortunately.
 
Ranch Hand
Posts: 1756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this format:
java -classpath dir filename

dir is the directory where the supporting classes are.
filename is the class file you will execute.

a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jar:. UseKit a b c

First of all, UseKit is in your current directory, test. So, it is ok to specify filename as UseKit.
It needs pkg.Kit or KitJar.jar to support UseKit.

A) By default, the current directory is test and pkg.Kit is in test directory. It works.
B) The classpath is com and current directory, test. In test directory, there is a pkg.Kit class. So it works.
C) The classpath is com/KitJar.jar, inside which the pkg.Kit resides. It works.
D) The classpath is com/KitJar.jar and current directory. It works in the same way as B) and C).
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Consider this format:
java -classpath dir filename

dir is the directory where the supporting classes are.
filename is the class file you will execute.

a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jar:. UseKit a b c

First of all, UseKit is in your current directory, test. So, it is ok to specify filename as UseKit.
It needs pkg.Kit or KitJar.jar to support UseKit.

A) By default, the current directory is test and pkg.Kit is in test directory. It works.
B) The classpath is com and current directory, test. In test directory, there is a pkg.Kit class. So it works.
C) The classpath is com/KitJar.jar, inside which the pkg.Kit resides. It works.
D) The classpath is com/KitJar.jar and current directory. It works in the same way as B) and C).



Finding a Jar file is a little different form find a class file, you must include the name of the Jar file in the end of the path.
In addition, if the classpath is specified as com/KitJar.jar, you have to add the current directory "." back.
 
Greenhorn
Posts: 12
Android VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First: you should be able to do this on Windows except that you will use the character ";" instead of ":" to delimit classpath entries.

Also I would give you one tip: make two versions of the Kit class. For instance, in the jar, package the class with a return "jar " + a + b and in the class file use a return "class " + a + b. To do so, create and compile the first version of the class, then create the jar file with this .class file. When you have your jar file, modify the .java file so it returns something else.

Next, on with the classpath basics:
. means current directory. If your current directory is test, and you look for UseKit.class, then the . will allow you to look in the current directory and find UseKit.class
: is used to delimit classpath entries on Unix & al. Use ; on Windows. For instance, use ".;com;tst" to look into the current directory and inside com and tst.
Also have a look at wikipedia.
When specifying a directory, class files inside that directory are used, but jar files won't. Jar files must be explicitly specified ("named").


Now for your questions:

a. java UseKit abc
Will cause a runtime exception, args[1] and args[2] are out of bound.


b. java -classpath com:. UseKit a b c
Will try to find te required classes inside the com directory first and then will try to find them in current directory "."

Heres, what the JVM will do:
1- The JVM looks for UseKit.class in the com directory - no such file
2- The JVM looks for UseKit.class in the current directory - class found
3- UseKit.class imports pkg.UseKit.
4- The JVM looks for pkg/Kit.class in the com directory - the subdirectory pkg does not exists.
5- The JVM looks for pkg/Kit.class in the current directory - the subdirectory pkg does exists and contains Kit.class - class found

The JVM now have everything it needs : ./UseKit.class and ./pkg/Kit.class and will give you the desired output.


c. java -classpath com/KitJar.jar UseKit a b c
Will try to find to required classes inside the Kit.jar file in the com directory.

Heres, what the JVM will do:
1- The JVM looks for UseKit.class in the KitJar.jar file - no such file
2- No more entry in the classpath, the JVM throws a java.lang.NoClassDefFoundError: UseKit


d. java -classpath com/KitJar.jar:. UseKit a b c
Will try to find to required classes inside the Kit.jar file in the com directory and then will try to find them in current directory "."

1- The JVM looks for UseKit.class in the KitJar.jar file - no such file
2- The JVM looks for UseKit.class in the current directory - class found
3- UseKit.class imports pkg.UseKit.
4- The JVM looks for pkg/Kit.class in the KitJar.tar file - in the KitJar.jar the subdirectory pkg does exists and contains Kit.class - class found

The JVM now have everything it needs : ./UseKit.class and ./com/KitJar.jar/pkg/Kit.class and will give you the desired output



Here's what you need to remember:
- The JVM will try to find its classes in each of the directory and jar file specified in the class *in the order they are specified in the classpath*
- The jar files need to be explicitly named in the classpath. If you want to use A.jar and B.jar, you MUST see them in the classpath.
- packages are "translated" to directory. The class com.java.Example must be inside "com/java". Your classpath must contains the folder *above* the com directory. The logic is simple: by positioning itself in the directory "above" com, the JVM will find the com folder, then the java folder, then the Example.class file.
- When no classpath is specified, the default classpath is used. This default classpath is in the environment variable $CLASSPATH on Unix and %CLASSPATH% on Windows.

Hope this helps.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!