Execute a jar inside another jar in java - java

I have an executable jar(mainjar) with me, i have created it using a maven project. When i pass some argument to his jar, it will execute another jar(execjar) file which is placed inside the src/main/resource of mainjar. The problem is i am not able to access the execjar inside the mainjar. Here is my folder structure of mainjar
src/main/java
MainClass
code to execute execjar- 'java -jar execjar'
src/main/resource/
execjar
The problem is when i package the mainjar using maven assembly plugin, i am not able to access the execjar inside the mainjar- nothing happens when i execute the mainjar.
Can anyone help ?

Look at the shade plugin to create an uberjar instead.
http://maven.apache.org/plugins/maven-shade-plugin/

Related

Create install anywhere launcher from executable jar

I have a jar file that is being created by Spring Boot. Application runs smoothly when run by command java -jar. I want to create an install anywhere launcher with this jar file.
What I have tried is to send the Spring Boot main class (PropertiesLauncher). The issue is that calling it like this won't load the nested jars inside my executable jar and also the loader.path doesn't seems to work.
Is there a way to call the executable jar like java -jar from the install anywhere launcher?
I was thinking that another option was to create an install anywhere launcher for a script file and inside have the java -jar call. So another question will be:
How do I create an install anywhere launcher for a script file?
'execute command' step will do the trick:
Use this command line:
java -jar <path.to.jar.file>
Use EXECUTE_STDOUT, EXECUTE_STDERR and EXECUTE_EXITCODE built-in variables to catch errors and parse the jar's execution result.
Important notes:
You'll have to make sure your jar includes all of the dependencies (or at least set the classpath in the command line);
To include the dependencies within your jar using eclipse you can:
Export your project as a 'runnable jar file' and select the
'Extract/Package required libraries into generated JAR' option/s
Use Maven to build the project with dependencies; the
maven-assembly-plugin is required.
The 'execute command' will work for batch/cmd/shell scripts as well, but you'll have to make sure the scripts are extracted to a local folder such as %TEMP% or /tmp before you can use them.
Goodluck

How to get my external jar in executable jar's classpath in eclipse?

I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It compiles, and if I run the program with no options, it executes fine, displaying usage information. However, if I actually pass in the parameters, it fails and says NoClassDefFoundError: com/sas/isd/midasapi/ParticipantDetailExt, which is in a jar I included as an external jar. I am confused that it compiled and runs to show usage information, but it fails to find the class after as the ParticipantDetailExt is a class that is imported. Wouldn't it identify that it was not found during compiling? How do I get it so that my on jar with ParticipantDetailExt is seen when I run my exectutable jar? Is there a classpath thing or pom thing I need to do in addition to the adding jar as external jar?
I assume that you are running mvn clean package or mvn clean install to create your jar.By default the jar created by a maven project does not include dependencies in that jar.
Option 1# create a jar-with-dependencies, see: How can I create an executable JAR with dependencies using Maven?.
Option 2# If you are just looking to copy dependencies to a lib folder see: http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html

Why doesn't an executable jar acknowledge jars inside it?

I made an executable jar that depends on other jars with the command prompt using the format
jar cvfm MyJarName.jar manifest.txt *.class dependentJar1.jar dependentJar2.jar
The jar was made properly and everything seemed fine... But when run, it gets runtime errors because it can't find the class files that my project refers to. Exploring the created jar, the other 2 jars that it depends on are in there just as they should be and they are listed in the manifest.mf class path, but for some reason java is dumb and doesn't actually look inside those jars.
When those 2 jars are in the same directory as the owner jar, java is able to find them and it works fine. But I don't want this; I want those jars to actually be INSIDE it. What's the deal? How can I make an executable jar with other jars inside it work?
You can use something like OneJar or jarjar (or any of several others) to pack up everything inside of a single jar.
If you're building with Maven you could use the Maven Shade Plugin.
You could use a custom classloader like JarClassLoader that will allow precisely what you want.
I don't think you could. Maybe it's somehow possible with customized classloader, but not from-the-box. Use maven shade plugin.
If you use an Ant Builder in Eclipse and put the referenced jars in a library accessible to your project, Eclipse will extract the necessary classes and include them in your distributable jar file.

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.

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