How Do I add dependency for Java? - java

I'm very new to Java. I have a single class file which is used to do some processing.
That class file is depended on a jar. I'm very new to Java, where I'm passing the jar to my classpath while running the program:
javac -classpath jar MyProgram.java
Now I wish to bundle both the jar and MyProgram into separate jar with dependency resolved.
Are there any ways to do this in Java? Note that it my MyProgram java is only around 50 lines of code, so some simple solution will be good.
Thanks in advance.

You cannot put the library JAR inside another JAR file - Java will not be able to load classes from embedded JAR files.
You could create a JAR file containing just the classes of your application, and have the library JAR alongside it. See Packaging Programs in JAR Files for details on how to do this exactly.
If you really want everything to be in a single JAR file, you could use a tool such as One-JAR to package it.

You should not bundle both your compiled code and the dependency in a separate jar. You should bundle only your compiled classes in a jar and when running the program, you put both your jar and the dependency in the classpath.
Use the jar command to build your jar file and then use the command below to run your program:
java -classpath dependecy.jar;yourjar.jar MyProgram

Related

Create JAR file of a specific package in a multi-package project

I understand how to create a JAR file when there is only a single package in a Java project but don't know how to create a JAR file when there are multiple packages in a Java project.
I have a sample project with the following structure:
I have two packages which you can see in the picture i.e.
academy.learnprogramming
academy.ujjwal
I want to create a jar file for the classes which are in the academy.ujjwal package. I tried multiple ways from the Artifacts option but not sure how to do it right.
It's usually uncommon to create jar file from command, Maven, Gradle like build tools are used but you can try following command.
jar cvp filename.jar /path/to/classes
jar cvf learnprogramming.jar src/academy.learnprogramming
jar cvp abc.jar src/academy.ujjwal

What are auto executable jars?

I was going through spring-boot-maven-plugin documentation and came across a term auto executable jar.
Could someone please explain me what is an auto executable jar and how is it different then normal jar files and how they are auto executed?
spring-boot-maven-plugin documentation mentions the term but does not go further to explain it
repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.
Could someone please explain me what is an auto executable jar
A fully executable jar can be executed like any other executable
binary or it can be registered with init.d or systemd. This makes it
very easy to install and manage Spring Boot applications in common
production environments.
So In conclusion is like any other executable when you use a executable jar
how is it different then normal jar files and how they are auto executed?
Well a java file you need to run with java -jar
From Spring Docs
The Maven build of a Springboot application first build your own application and pack it into a JAR file.
In the second stage (repackage) it will wrap that jar with all the jar files from the dependency tree into a new wrapper jar archive. It will also generate a Manifest file where is defined what's the application Main class is (also in the wrapper jar).
After mvn package you can also see 2 jar files in your target directory. The original file and the wrapped jar file.
You can start a Springboot application with a simple command like:
java -jar my-springboot-app.jar
I may suggest that auto executable means that you supplied main method so that it can be launched with java -jar options, otherwise it may be just a java library.
Here is a quote from https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).
Executable jar - the one that has main class declared in manifest and can be run with java -jar yourJarFile.jar command
Other jars - jars jars without delcared main calss. Can be anything - application, library, etc. Still can run application by providing fully.qualified.class.name as entry point like java -cp yourJarFile.jar my.bootstrap.BootstrapClass
Autoexecutable jars - never heard about it :)

How to build a distributable jar with Ant for a java project having external jar dependencies

I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.

Load a config file into the classpath while using an executable jar

I am building an assembly in Maven for a command line utility. I can run it as an executable jar, but it fails because I need to load the config file externally. Assuming the following config, how would I run the jar?
Jar is in /opt/myapp/lib/myapp-assembly.jar
Config is in /etc/myapp/config/settings.xml
I'm loading the code from the classpath using ClassPathResource("/settings.xml");
Any help is appreciated!
I see two ways you could do it:
Launch the program using the jar as an archive rather than an executable jar, specifying the main class at run time. In other words, do java -classpath /opt/myapp/lib/myapp-assembly.jar:/etc/myapp/config [name of your main class].
Use the Class-Path field of the jar manifest file. Entries in it are directly added to the run time classpath, and there's nothing stopping you from specifying a filesystem directory rather than another jar file. So your manifest would contain: Class-Path: /etc/myapp/config/

Using JAR inside a jar

I created a JAR file from my java project.
Using Eclipse, I added a JAR as a referenced library in my own project.
However, now when I try to run my program's JAR using java -jar myProgram.jar, I get an exception stating that my referenced jar is not available.
So how can I create a JAR consisting a reference to a different JAR and make it work?
Right, an executable JAR cannot contain its own JAR dependencies.
You have to have the main class and classpath set in the executable JAR manifest, then package all your JAR dependencies along with the executable JAR in a relative directory structure that matches the manifest CLASSPATH. Reading this might help.
You need to use Eclipse's runnable JAR exporter. Since Eclipse 3.5 you've the following options when you rightclick project, choose Export > Runnable JAR file:
Either way, Eclipse should take care that you'll be able to run the JAR the way you want on the exported location.
See jarjar project. It is exactly what you are looking for. http://code.google.com/p/jarjar/

Categories

Resources