my dog learned polymorphism*
The moose likes Beginning Java and the fly likes I still can't do this 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 » Java » Beginning Java
Bookmark "I still can Watch "I still can New topic
Author

I still can't do this

Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
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

Joined: Jul 21, 2011
Posts: 4

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


André Bensoussan once explained the difference between a programmer and a designer:

"If you make a general statement, a programmer says, 'Yes, but...'
while a designer says, 'Yes, and...'"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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
Bartender

Joined: May 31, 2011
Posts: 1776
Rob Spoor wrote:javac -cp jr.jar;. MyClass.java

Yikes... he has already mentioned that
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Only for javac, not for java. So your post definitely added something.
Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Jul 29, 2011
Posts: 31
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I still can't do this