• 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

JAR - Package directory - can't find or load main method

 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand how package(directory structure) and JAR files work interchangeably. From my understanding JAR is a package file format typically used to aggregate many Java classes. Since many classes are usually contained inside a package, I should be able to create a JAR file from a package, and use them interchangeably. A single JAR file is easier to handle then multiple classes and folders.

To test my understanding of the concept, I am working with the following project structure,

/development_directory
____/BookPackage
________Book.java
____BookDemo.java


Book.java contains the following code,

and BookDemo.java contains the following code,


From the development_directory, I used the following commands to compile and run the project,
development_directory/ javac BookDemo.java
development_directory/ java BookDemo

...and it compiles and works as expected.

-------------------------------------------------------------------------------------------------

Now, I will try to convert the BookPackage folder(package) into single jar file for portability, and I am using the following command to do that,
development_directory/ jar -cf bookpkg.jar BookPackage
...this command generates the bookpkg.jar file.

I am going to remove the BookPackage folder to see if bookpkg.jar file really works as a replacement for the BookPackage directory structure.

After removal of BookPackage folder, the development_directory structure looks like this,
/development_directory
____bookpkg.jar
____BookDemo.java


Now, I tried to use the following commands to see if the program works as before,

// to compile
development_directory/ javac -cp ./bookpkg.jar BookDemo.java
...it compiles without any error - this means the replacement of bookpkg.jar for BookPackage(package) folder structure is working - Am I correct?

// to run
development_directory/ java -cp ./bookpkg.jar BookDemo
...And I hit an error,

Error: Could not find or load main class BookDemo

But the BookDemo class is at default namespace - why it JRE won't be able to find the main method?

And, the way I am trying to replace package folder structure with a single JAR file - am I getting the concept correctly?

-
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class path in the java call only contains the jar file.
It does not contain '.' (ie the current directory) so the JVM can't find the BookDemo class.
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave. I used the following command and it works,



I found the explanation in another thread : Classpath and 'Current Directory'

So, I am getting the concept or Package and JAR correctly, right?
 
reply
    Bookmark Topic Watch Topic
  • New Topic