• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I still can't do this

 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel so dumb right now. I have a jar, called jr. jar, and it is in a directory on my computer C:\JavaSrc. In this jar, is a class called GDate I want to use. To use it, I have to add the jar to the classpath when I type javac in the command window. I am using windows. Nothing I tried works. What do I have to type into the command window to make it add the jar file to the classpath? And what do I have to type into the source file I am trying to compile to get it to recognise the class GDate? I have tried to find out on my own but to no avail. I need to type something into my computer that works. Please, could someone be kind enough inform me on how to do this?
 
srikanth kumar aruru
Greenhorn
Posts: 4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) extract the jar file and see the directory/package structure, accordingly import GDate class in your java source code.
2) Either put this jr.jar in existing classpath or add C:\JavaSrc to the class path.
3) to add C:\JavaSrc to the existing class path, on windows machine, go to Start > Settings > Control Panel > System, and click on advanced , add the path there
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Thvedt wrote:What do I have to type into the command window to make it add the jar file to the classpath?

Use the -cp or -classpath flag: javac -cp jr.jar;. MyClass.java

And what do I have to type into the source file I am trying to compile to get it to recognise the class GDate?

You need to import it - somewhere between the package declaration (if present) and the class name, you need to add an import statement for the class. That needs to include the full class name, with the package statement of the class.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And while running using java command use like below -

java -cp C:/test/MyJar.jar;. Hai

where the classpath points to both the jar file and the present directory (.) where the Hai class is present.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:javac -cp jr.jar;. MyClass.java

Yikes... he has already mentioned that
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only for javac, not for java. So your post definitely added something.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! I just couldn't tie everything together. So to recap: My class is called Test, in that class I want to use a class called GDate located in a package contained in the jar file jr.jar. Both test and jr.jar are in the same folder, and I am using Windows. To make it work:

1.) Find out the package name. I gotta know this. The full name seems to be com.javaranch.common. Then in the code for Test include it in an import statement by typing: "import com.javaranch.common.GDate;"
2.) Add jr. jar to the classpath by typing "javac -cp jr.jar.; Test.java" in the command window
3.) When trying to run also include the classpath. in other words just typing "java Test" won't work. I gotta type: "java -cp jr.jar.; Test" for it to run. I might never have gotten this part on my own.

Ok! I got it working. It was a catharthic experience. I shall now continue my fun adventures learning about classpaths and packages, thanks to you guys!
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to change .; into ;., but other than that you're right. You see, the ; is used to separate the two class path entries jr.jar and ., with the latter meaning the current directory.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You need to change .; into ;., but other than that you're right. You see, the ; is used to separate the two class path entries jr.jar and ., with the latter meaning the current directory.


Thanks for the tip! That would have probably caused me problems later.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic