gabi13
gabi13

Reputation: 91

Build .jar with .class and others .jar

I have the .class generated in compiling a .java file. The whole code of my program is in a main within my .java. With this, from the command line I want to create a .jar to execute directly from the command line with JAVA-jar. In my program I use two external .jar files, which I have in the same folder as the other files, but I do not know how to put them in the process of creating or executing the .jar. My script contains the following:

  jar  -cf CantidadAnio.jar CantidadAnio.class
  @ECHO.
  @ECHO.
  jar cmf temp.mf CantidadAnio.jar CantidadAnio.class
  JAVA-jar CantidadAnio.jar cantidadanio.csv

temp.mf: Main-Class: CantidadAnio Sealed: true

The problem is in the execution of .jar, which does not recognize the classes and methods that are defined in the two external .jar

I have also tried to generate and run the .class from the .java, but at the time of execution it shows me the error: "Could not find or load main class CantidadAnio"

For this I have used the script:

 javac -cp "opencsv-3.9.jar;ChartDirector.jar" CantidadAnio.java
java -cp "opencsv-3.9.jar;ChartDirector.jar" CantidadAnio "cantidadanio.csv"

Upvotes: 2

Views: 1043

Answers (1)

Lew Bloch
Lew Bloch

Reputation: 3433

It's usually a bad idea to package third-party JARs into one's own custom JAR. Read up on the JAR manifest, which specifies the classpath for a java -jar execution, among other things. The usual approach is to package the third-party JARs with your JAR in the same directory, or in a lib/ subdirectory, and use either Java Web Start/JNLP or a ZIP file to deliver the whole package. In the end you have a deployment that might look like this:

deployment_directory/
|
|-- your.jar
|
|-- lib/
    |
    |-- third_party.jar
    |
    |-- other_third_party.jar

Then you use the Class-Path: manifest entry in your JAR to tell where the other JARs are, relative to the deployment directory.

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javaws.html#BABHGIBB https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/jnlp.html https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jar.html#BGBEJEEG http://docs.oracle.com/javase/tutorial/deployment/jar/index.html

Upvotes: 0

Related Questions