This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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...'"
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.
Only for javac, not for java. So your post definitely added something.
Joined: Jul 29, 2011
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!
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.
Joined: Jul 29, 2011
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.