I'll try to boil my issue down into a basic external JAR linking question, which I have not been able to find an example/answer for. I have 2 JARs, a.jar and b.jar, in the same directory. The MANIFEST.MF of a.jar contains: Class-Path: b.jar.
Essentially, I want to run a.jar that contains minimal application-level classes, but links to a large external b.jar with all other necessary classes. But running the command java -jar a.jar results in: Exception in thread "main" java.lang.NoClassDefFoundError: com/example/MainApp.
Not sure if it's relevant, but b.jar is actually a Spring Boot JAR which contains the expected classes (e.g. com/example/MainApp.class) in BOOT-INF/classes. The MANIFEST.MF of b.jar contains: Spring-Boot-Classes: BOOT-INF/classes/.
I want to know if there's a simple way to achieve this? Let me know if any more details are needed to diagnose the issue.
Update:
I copied the com/example/MainApp.class file to the base directory in b.jar, and the class was found! But I want to keep the original files in place. So I suppose that rephrases the question: how do you specify where classes are located inside the JAR?
did you try something like that path like "classpath: path/to/jar/b.jar"
Related
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 :)
I'm having Spring based web-app that I'm trying to bundle into a single jar.
My thoughts on laying out the project was to have a root project that contains multiple sub-projects, where there would be one sub-project that is a spring-boot application, and others sub-projects maybe my own-written code handle certain business logic let's say.
So the structure may look something like:
/root
build.gradle <- the problem is here, how I should write root project build script
/spring-backend-subproject
/other-business-subproject
Having all these sub-projects, I want to package them all into one single JAR, so that I can just do
java -jar build/libs/RootJar.jar
otherwise I need to run:
./gradlew bootRun
which will run the particular main class in the "spring-boot" sub-project
I've been searching online for how to bundle all subproject jars into one jar, such as this one: Can Gradle jar multiple projects into one jar?
but I often just end up having errors such as main-class not found(which does not happen if I run the jar specifically in the "spring sub-project" folder) or SpringApplication class not found (again does not happen if run jar in the sub-project folder)
How may I solve this problem?
Thanks!
I use this pattern extensively. Instead of trying to put the deployment classes into the root, add a module (I usually call it -launcher) that lists the other modules as dependencies and contains your main class and related application code, such as any application.properties you may have.
This module will produce an artifact my-project-launcher.jar, and you deploy this to whatever platform you're using.
I have seen this question asked a lot, but I still can't figure out a solution to it. Well a solution that works for me. I have a project that is using Apache POI, and I made sure to include all the external JARs. The project compiles and runs fine in eclipse but when I run the jar with "java -jar Test.jar" I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Sheet
I'm not sure if this is useful information, but I created a lib folder for my project and put the poi library in there. This means that the dependcies are in the JAR file when I create it, I figured I should mention this because I saw a few solutions about just having your external jars right next to your executable jar. I also tried setting my classpath to the directory of the project.
What do I seem to be doing wrong?
The Apache POI JAR file is not on your runtime classpath. Rebuild Test.jar with the following manifest entry in the MANIFEST.MF file
Class-Path: poi-3.9-20121203.jar
When the java -jar [filename].jar command fails, it's almost always because of one of two things:
Your MANIFEST.MF is messed up and doesn't list dependencies properly. Make sure all jar dependencies in your manifest file point to jars, relative to your jar's parent directory.
You are missing .class files, either in your specific jar or in one you depend on. Ensure your jar contains org/apache/poi/ss/usermodel/Sheet.class or that your manifest hierarchy points to a jar that contains that class.
You will need to provide classpath in Jar file's manifest file. See this official doc
An Example
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
First check if your target jar (Test.jar) is a fat jar containing all the required dependencies.
$ jar tf Test.jar
You should see your lib/ folder there containing all the required dependencies (including Apache POI). If that's the case do what others suggested, add Apache POI to your MANIFEST.MF.
About having your jar dependencies in a separate folder, as you suggested, that's also possible. Imagine your dependencies where stored in a lib/ folder outside your Test.jar. You could run your code with this command:
$ java -cp ".:lib/*" org.Test.Main
It simply adds all the .jars within lib/ folder to your classpath. In this case you also need to specify the name of the main class (full name).
I also got this problem and tried to google it..
i have found out that i have to read the error log
I cant save my file to .xls so
in my case after reading the error i found that a jar file is missing
i just added the jar file poi-3.7-20101029 located in the ext folder of my ireport
ex.YourIReportFolder/ireport/modules/ext-poi.x.x-xxxx
hope this helps :)
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.
My goal is pretty simple: to use ant to build an EAR which contains 1 EJB and 1 jar containing all of the dependencies. This jar, called common.jar for the sake of example has vendor jar files in it as well as other xml files that the EJB depends on and will need to be able to see during runtime....
So far I have everything packaged correctly as an EAR like this:
EARFILE.ear
-EJBFILE.jar
/META-INF
-MANIFEST.MF
-common.jar
/META-INF
-MANIFEST.MF
/lib
-(all vendor jars inside here)
-(All the xml config files are inside the root of the common.jar)
Inside the MANIFEST.MF for the EJBFILE.jar is...
Class-path: ../../common.jar
Inside the MANIFEST.MF for the common.jar is...
Class-path: ../lib/some_common.jar
When I deploy this the appserver (websphere) cannot find the JAR file when I try to start the server. I am getting the ClassDefNotFoundError because the classes inside the EJB cant find the vendor JAR files when I try to start the instance. However I know that common.jar is setup correctly though, else the EJB wouldn't have compiled since it needed to have those vendor jars on the classpath for javac.
So what I want to know is this:
How can I get the runtime to correctly see the Vendor jar files.
Will the EJB be able to see the xml files at run-time? I am concerned about this because these xml files are located outside of the EJB inside of a jar that is just in the EAR, it isn't even a module its just a jar inside the EAR.
Does it even matter when using websphere? From what I gather some containers don't even care what is in the Class-path of MANIFEST.MF.
There are several improvements I can suggest, based on running into similar problems.
First and most importantly, use the appxml attribute of the Ant ear task to specify your deployment descriptor (usually named application.xml); also include references to the vendor JAR files bundled as defined below
I would recommend you not put your vendor JAR files into another JAR - instead, just copy them into the EAR at the same level as EJBFILE.jar
The configuration XML files can go in a sub-directory of the EJBFILE.jar (such as config), and then you can reference them as /config/filename.xml.
The application.xml file will tell WebSphere where to find your JAR files. Classpath traversal in an application server is not the same as that of a compiler, which JBoss has taught me the hard way.
I am using all of the above patterns, and my in-container code (deployed in the EAR) can see all my XML files, as well as find all my dependencies.