I have a jar file that references 6 other jars through a manifest file.
I now, however, want to try and compile the jars into one jar file, reasons being: I want them to be cached so the applet does not have a long loading time as one of those jars are required, rather one big waiting time to download all required files and then store it in the cache.
I have tried including the referenced Jar files in the build, unfortunately, all this does is cause a java.lang.NoClassDefFoundError error, so I had to add a manifest file.
What is the best/easiest way to achieve this? I am using eclipse, and not building with ant.
Using eclipse while exporting you can create with the option of
Package required libraries into generate JAR
By doing this eclipse uses its own class loader and reference all the files which are packed in a single jar.
Related
I am working with Scala in Intellij-Idea, and have run into a problem. I have included the Java tag in case someone with Java experience can also answer my question.
In my .ivy2/cache folder for a certain dependency I am working with, there are two jar files. One contains .class files and one contains .scala source files. I want to be able to modify one of the jars, drop it in my lib folder, then run my program with the jar that includes my changes.
I have an issue though. I can modify the file in the source folder with jar uvf, but when I drop it in lib, and run it, the changes don't show up. I have tried commenting out the dependency in my build.sbt file, but it appears to still be using my old dependency even after I run sbt update.
My question can be divided into several parts:
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
I use Intellij only to edit my files, and use sbt to run the code.
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
That's not possible because the jar file is expected to contain class files, scala files will be handled as resources instead of source files.
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
I would run sbt clean instead, I haven't tried but sbt "show unmanagedBase" is supposed to list you the library if it was actually picked.
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
The ideal way would be to find the complete repository for that source, update the source and run sbt publishLocal, if that's not possible, another way would be to reconstruct the build.sbt and keep adding libraries until the library compiles properly, hopefully, it will have no dependencies, or a few only.
Difference between extracting and packaging libraries into a jar file explains what happens if you select each option respectively.
What I don't understand is when one option would be recommended above the other.
Extract required libraries into JAR - Extracts the actual .class files from the libraries your app uses and puts those .class files inside the runnable JAR. So, the runnable JAR will not only contain the .class files of your application, but also the .class files of all the libraries your application uses.
If the JARs are kept separate, then the user would only have to download the JAR that contains your application code, instead of a single, massive JAR that contains your application code and all the library code.
if you update your application, then the user will have to download more data to update the application.
1)Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible.
2)Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the .class files of your application.
How do I include all the jars into Launch4J so that I can build an exe without any dependencies?
Thanks.
I think the trick here is to package all of your class files and all dependencies within a single jar. This means extracting the contents of those dependent jars, and then repackaging them.
The answer to this question appears to be related and might help: Launch4J - how to attach dependent jars to generated exe
I think an entirely manual process is also possible. Compile your class file into a build directory. Open each dependency (jar file) as if it were a plain ZIP, and extract the contents into the directory with your class files. Repeat this for every jar. Now your code and the dependencies sit side by side - package the entire set of files into a single JAR and proceed with the steps needed to package that up with Launch4J.
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
Kind of a "simple" question here, but it seems complicated to do such a thing using Eclipse.
I have a "utils" project, in which I have developped "common" code like xml parsers, loggers, maths calculations, debug utilities and such.
This library is based on several other external libs (commons-lang-3.1, colt-1.2.0, jdom-2.0.4) to work, and it's a non-runnable JAR file (ie, there is no main(), just utility code to include in other projects).
What I want to do is, when I create the JAR file of my project, extract all external JARs (common-lang, colt, jdom) in the JAR file, in order to automatically use them on other projects.
The reason is that I don't want to re-include common-lang, colt and jdom on every projects based on my lib, but use the ones packed in my lib JAR file (and it's a way to ensure that I'll be using the same version of those libs in the projects based on my lib, too)
I know how to do that with a runnable JAR file.
See for instance : How to create a jar with external libraries included in Eclipse?
But in a non-runnable JAR file :
all my classes are correcly exported in a correct directory structure based on the packages I created (hopefully ;-)),
but external JAR files are exported "as-is", so when I import my JAR in another project, those JARs (common-lang, ...) can't be seen, and I have to re-import them.
Is there a simple way to export this JAR file with a directory structure in which all external JARs files are extracted in the generated JAR file, too ?
Regards
Have you seen JarSplice?
JarSplice merge all your jars and native files into one, easy to use executable jar file via an easy to use GUI.