Inclusion of libraries in the bank

So, here's what we got:

We have two library packages that we have collected in banks.

  package starwars; 
 public class JarJar {

     public void JarSayHello ()
     {
         System.out.println ("Jaaaaar !!"); 
     }

 }

  package barwars; 
 public class BarBar {

     public void BarSayHello ()
     {
         System.out.println ("Baaaaa !!"); 
     }

 }

Compile them with

  javac -d bin -sourcepath src src / barwars / BarBar.java
 jar cvf barwars.jar -C bin. 

and

  javac -d bin -sourcepath src src / starwars / JarJar.java
 jar cvf starwars.jar -C bin. 

Everything is good for cans for us.

Now we want to include these two jars in another java project.

therefore we have

  • /project/src/a_pack/HelloWorld.java
  • /project/libs/starwars.jar
  • /project/libs/barwars.jar
  • /project/manifest.txt
  package a_pack;
 import starwars.JarJar; 
 import barwars.BarBar; 

 public class HelloWorld {

     public static void main (String [] args) {    
         System.out.println ("Hello, World");         
         JarJar myJar = new JarJar ();
         myJar.JarSayHello ();        
         BarBar myBar = new BarBar (); 
         myBar.BarSayHello (); 
     }

 }

Manifest.txt

  Main-Class: a_pack.HelloWorld
 Class-Path: libs / starwars.jar libs / barwars.jar

Now we compile this with:

  javac -d bin -sourcepath src -cp "libs / starwars.jar; libs / *" src / a_pack / HelloWorld.java 
 jar cvfm helloworld.jar manifest.txt -C bin. 

And it compiles and works fine.

Now I have two problems.

Firstly - if I move this jar file to another location and try to run it, I will get:

Exception in thread "main" java.lang.NoClassDefFoundError: starwars/JarJar

Now I can fix this by moving the libs folder to where I am moving the jar. But it seems to me useless (what if there is already a libs folder in this place?).

Ideally, what I would like to do is to include the mentioned banks in the bank, so there is one bank that contains everything you need to run inside yourself.

Is it possible? (And is this a good practice?)

+6
source share
1 answer

Probably yes. Good practice, no.

Jars are just zip files, so you can unzip and redraw the contents of your heart. The big problem is managing all these individual banks as your project gets bigger.

Most projects do not compile using the command line. Instead, the IDE keeps your banks up to date. And most modern Java projects use Maven or Ivy to place jars in the repository and tear them out as needed.

Take a look at Eclipse, Netbeans, or Intellij for the IDE. And take a look at Maven to structure your project.

+7
source

Source: https://habr.com/ru/post/945898/


All Articles