• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I still can't do this

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:javac -cp jr.jar;. MyClass.java


Yikes... he has already mentioned that
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic