• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Stuck on 'Compiling with JAR Files' in Java SE11 Study Guide

 
Sarah Holland
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have got the first certification for SE8 and want to go on to 11 so have bought the study guide for the first exam.  I am on the section about compiling with JAR files and I think I have just got myself confused as I saved my files in a different folder to what the book says so that I knew I would find them again!

It states on macOS to type:

java -cp ".:/tmp/someOtherLocation:/tmp/myJar.jar" myPackage.MyClass

I'm unsure about which of these parts are typed regardless of location and which of these would need to be changed for my own files - eg I have saved in Documents > Java11 and my package and class are called packageb and ClassB.  Or is there something that I need to do before typing this as no matter what parts I switch out it says it cannot find or load main class.

I think I have just got myself lost on something simple here!
 
Campbell Ritchie
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry you have had such a long wait for a reply. Have you been through the Java™ Tutorials? You don't need the whole of that section: this part might also be useful. What you are doing is adding a resource to your CLASSPATH. You have three entries in that CLASSPATH. Only the . part, referring to your current directory, will be unchanged. You would usually have to change the remainder to match the locations of your resources. Start with one resource and . only, to make things easier.

Remember that if you have multiple XXX.jar files in your CLASSPATH, and they contain multiple copies of XYZ.class files with the same name, you can't predict which XYZ.class file the JVM will use.

[edit]Remember the Java™ Tutorials haven't been updated since Java8, so you won't find anything about modules there.
 
Stephan van Hulst
Saloon Keeper
Posts: 14267
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a simple application that doesn't have any dependencies, the only path you need to provide to the -cp switch is the folder where your compiled class files are located, or the path to the JAR file if you built one.

If your application has dependencies, it's easier to use Maven. Maven will set the class path for you.
 
Jesse Duncan
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your command, you seem to have already compiled your code, and are now trying to run it. If you are trying to compile, you need the javac tool, not the java tool.

For the 'java' command — to run your program — you have to be in the directory containing the package your main class is in. If the class you want to run has
at the top, you have to run it from the directory containing the foo directory.

As far as the classpath is concerned, all you are trying to do is add all of the compiled resources. It doesn't matter where they are. You know where all of your files are, so just put
1. all directories containing .class files you need and
2. all needed jar files
onto the classpath. Note that for a jarfile, you actually have to add the file to your classpath, as they are not loaded with a directory.

I assume you already know how to add things to the classpath; just '-cp' followed by the locations of the files you need separated by a colon ( : ), like in the example you provided
 
Sarah Holland
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies!

I have looked through the tutorials and with your replies I think I just about understand it now, until I get the the modules section at least.

Many thanks everyone
 
Campbell Ritchie
Marshal
Posts: 76412
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Holland wrote:Thanks for your replies!

On everybody's behalf, that's a pleasure

. . . until I get the the modules section at least. . . .

As I said, there isn't a modules section that I know about.
 
I do some of my very best work in water. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic