Eclipse - Extract/package the required libraries into the same Runnable JAR - java

When exporting a project as a Runnable JAR file using Eclipse, there are three choices:
1. Extract required libraries into generated JAR
2. Package required libraries into generated JAR
3. Copy required libraries into a sub-folder next to the generated JAR
However, it seems that only one of the three choices could be chosen from the Runnable JAR File Export window. I wonder if there are some ways that could mix up these methods, e.g., extracting some of the files while packaging the rest?
Note: I need this since one of my JAR file contains a ton of references to other files so 1 cannot be an option, and I am afraid that 2 might be causing some problems for me.
Thanks!

What you can do is choose "1. Extract required libraries into generated JAR" and save the build as an Ant script. Then after it finishes, edit the ant script and remove the libraries that you do not want to be extracted.

Related

Create Java projects without eclipse?

Is there any way I can create Java projects using a simple text editor? Not an IDE like eclipse?
I want to be able to create .jar files without the assistance of an IDE, I have all the JDK commands installed already on my computer (such as javac)
I'd like to know what file structure I need, how I need to arrange my class files, what compilation steps I need to go through etc. to be able to create jar files.
Yes, completely doable (just not much fun once the project gets bigger).
I suggest if it's not a throwaway project, use a build tool like Maven or Gradle to manage your build process, so that you don't need to assemble classpaths and resources yourself, but still retain full control of the build and test lifecycle, without IDEs. This comes at a complexity cost, of course, but once it's set up life becomes easier.
See also How can I create an executable JAR with dependencies using Maven? or the Gradle docs about creating JARs
I'd highly recommend the standard Maven source directory layout too (src/main, src/test etc) as it's both commonplace and makes for easy integration with the above tools.
Follow the below steps to create a jar file
Compile the java source using javac
Create a manifest file (if main exists) to identify main class
Use the below command to create a jar file
jar -cvfm *.class
Yeah. You can create your project structure without an IDE. But it's time consuming and you have to do everything.
To talk about creating JAR, you don't want any extra software. You can use jar utility, which comes with JDK.
Here are steps to create jar:
Compile classes which you want to in jar
Create manifest file (.mf). It's needed if you want to make jar as executable. If you want to bundle classes only together, then no need. (eg. Dependency jar)
Go to command prompt and execute following command "jar cvf MyJarName.jar *.class". Make sure java is set in environment path and you're inside the directory of classes.
cvf means "create a jar; show verbose output; specify the output jar file name.
That's all. If you want to include any folders inside jar then you can use folder name in above command after classes and it must be separated by space.
Example: jar cvf TicTacToe.jar TicTacToe.class audio images

Eclipse access to workspace, compiler and export function from code

I have following tasks:
Automatically generate java source files in the current workspace.
Compile those files after generation.
Export every generated and compiled class with needed libraries to runnable JAR file.
I already installed Eclipse SDK and I suppose what I need is to make my main class inherit some class from SDK and maybe load some other classes. But i don't know what do I need exactly and where to look. I'd appreciate some clues.
I suggest you look at M2T-JET to generate not only the Java files, but the project, any necessary folders and any other resources you need. One of those resources would be a jardesc file which is used by the JDT to persist jar export options. You can play around with those options to define the jar and export, then generate the jardesc file along with the other generated resources.
M2T-JET can be invoked programmatically, so once that single invocation generates your entire project, your plugin can make the call to the JDT to export the jar using the jardesc file.

Classpath and portability

I am new to Java and am using Eclipse to write Java code.
I've added lots of library (.jar) files as referenced libraries. I've also exported my project as a JAR file. My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
I also made a runnable JAR file, whose size was much larger (~29 MB) as compared to the previous file (~24 KB).
My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
No. You need to package the referenced JARs alongside your program JAR. It would be inconvenient for the end-user to have to download all the libraries that your program depends on. A couple of options, both possible using the Eclipse Export function.
Unpack all your dependent JARs and package them together with your code into one single executable JAR.
Specify in your program JAR manifest the classpath, which will contain relative paths to the location of your dependent JARs. These could be for example in a lib folder. The location of the lib folder relative to your program JAR would need to be the same for all your end-users, so it would make sense to package it all together.
It depends on the way you package jars with the runnable jar app. if you do it inside the jar or outside it then you should make a classpath entry to your manifest.mf.
you can even bundle the reference library in the same jar. So as you reduced the size to that extent i assume you removed those from the jar, which is not good approach in most cases. It is not recommended not to include referenced jar, unless you are 100% (not even 99.9%) sure those library WILL be in the system you would want to run.
EDIT:
to include the referenced jar in eclipse, you need to goto
project-properties>java-build-path>order-and-export
here check whatever libraries you want.

Different jar export sizes

I have a question with exporting jar files in Eclipse.
When I export my program as a runnable jar on my desktop it is ~16mb. When I export the same program from my laptop it is less than 1mb. It still runs perfectly when exported from my laptop as well. It is a tiny program and I was shocked when it was 15mb.
Is my desktop putting too many libraries or something in my jar file?
What should I do to only export what my program needs?
If you have the program set up on both computers, it may be that the build environment is different on each.
Right click on the project in the package explorer, select "Build Path..." -> "Configure Build Path..." and then "Libraries". Now, compare the libraries between the two computers.
If the project is working on your laptop, correlate the libraries and only select those that are required.
I also want to highly recommend using Maven with the Shade plugin rather then managing your projects through Eclipse. In this way you will (should?) never have this problem, while still having an executable jar. Moving between IDEs will also be straightforward.
Check your export options. The second dialog provides three radio boxes. Try the second one that called "Package required libraries into generated JAR". The generated jar is smaller than the other generated with the first option.
By the way... you can take a look into a jar file if you using Winzip, Winrar, Totalcommander, etc. The reason is thar the algorythm to generating a jar, war or ear file is similar to zip algorythm.

how i bundle my program with JGoodies Extension

i wrote an application with java , and i use JGoodies in my code .
when i export jar file , that file doesn't run in another systems .
how can i bundle my program with JGoodies jar files ?
There are essentially two ways to do this, but it all boils down to making sure that your classes and the JGoodies classes are both in the classpath when the user runs the application.
The first way is to simply distribute your jar file along with the JGoodies jar file, and any jar files that either of them depend upon. The user then must include all (both) of those jars in their classpath with, the '-cp' option to the java command line.
The second option is to re-bundle all of the classes into one big jar file (usually referred to as an 'uber-jar'). If your build uses Maven, or you can convert to Maven, you can use the Maven Assembly Plugin - http://maven.apache.org/plugins/maven-assembly-plugin/

Categories

Resources