Pack all dependent jar along with jar creation (command line) - java

I am trying to create a jar using the
"jar cfe ..."
command.
However Iam unable to find a way to include all the dependent jar along with my build.Something like "Extract required libraries to generated jar" option in eclipse jar creation.
Please help if anyone knows how to do this in command line.
Edit:
I tried adding the jars as input-files ( docs.oracle.com/javase/tutorial/deployment/jar/build.html) but while running the jar i got ClassNotFoundException on the depended jars.
I also tried mentioning the jars in Classpath of manifest file,but that way i still have to put all the dependent jars in a file directory along with mg main jar.

Related

Exception: com.mysql.jdbc.Driver | Eclipse isnt exporting jar

again me.
I dont know what the hell isnt working right, but everytime im exporting my Teamspeak Bot wrote in Java, it seems like he dont export the mysql-connector.jar.
Everytime im trying to start my jar, he always tells me that he cant find the MySQL-Driver.
Heres my Code:
As you can see, the jar file is in the buildpath. When i try running it through Eclipse, everything works fine. Only when im exporting he throws me the Exception.
Hope someone of you can help me.
ndslr
The way you're exporting your jar, your library jar isn't automatically added to the output jar. You would have to specify the classpath when you run it:
java -cp "C:\Users\Andy\Desktop\mysql-connector.jar;YourOutputJar.jar" TS3BotMySQL
Instead, you have to add the mysql-connector.jar to your output jar. To do this, instead of going to export > jar, go to export > runnable jar, and then select "Extract required libraries into generated JAR" or "Package required libraries into generated JAR" at the prompt.
See these related questions for more information:
How to create a jar with external libraries included in Eclipse?
Eclipse Java; export jar, include referenced libraries, without fatjar
Generating a Jar in Eclipse including external library
How export Java jar from Eclipse with dependencies on other jars?

How to create Jar file with external folders and Jars

I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html

jar file creation - library dependecy

i want to create a jar file of my java web application.
This web application has so many jar files related to this. so all am including in the build path. And when the time of jar file creation from eclipse,even if i had checked the checkbox for exporting the class files and resources option, the generated jar file doesn't have those library files. So when am trying to execute the jar file from command line option by using
java -jar myJarfile.jar
it will throw error like NoClassDefFoundError , can anyone help me to solve the issue.?
How can i generate the jar with all dependencies.?
You shouldn't really be including external dependencies in your jar. Just bundle up your code into your jar and specify the dependencies at runtime.
java -jar myJarfile.jar -cp someDependency.jar

Using external Jar dependency in Eclipse with Non-Runnable Jar

I am making a Jar file in Eclipse to run an Applet, and the program uses an external jar file (jxl.jar). When I run the applet, it tells me it can't find the jxl files I am trying to use.
When I make the jar file, I right click the project and select Export and then JAR file. I don't see an option in the dialog that comes up to include dependencies. How can I ensure that it is including my external jar file, which is added to the external Java Build Path as an External Jar file in my Project Properties.
There are two ways to do this:
Specify the dependent JAR in the "archive" parameter; e.g.
ARCHIVE="myapplet.jar,jxl.jar"
Specify the dependent JAR in the manifest of your applet JAR; e.g. in the manifest.mf file add this line:
Class-Path: jxl.jar

java eclipse create executable jar

I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.
But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]
It can generate jar. But the jar can only be executed in the directory where it is produced.
If I put it into another directory or machine, it complains NoClassDefFoundError.
So, my question is that how I can generate executable jar using command line as Eclipse.
A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to
1) Create a manifest file which lists all the dependency jars
Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
dependency4.jar dependency5.jar
2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.
3) In this same folder where you created your jar, place all the dependency jars.
Now your folder will look like this,
yourjar.jar (With the manifest file you created above)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar
4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.
Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :
so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.
My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post

Categories