The simplest way to create jar file? - java

I have a java project and i need to create jar file from my project . any body can tell me what is the simplest way to make this?

The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file.
The f option indicates that you want the output to go to a file
rather than to stdout.
jar-file is the name that you want the resulting JAR file to have.
You can use any filename for a JAR file. By convention, JAR filenames
are given a .jar extension, though this is not required.
The input-file(s) argument is a space-separated list of one or more
files that you want to include in your JAR file. The input-file(s)
argument can contain the wildcard * symbol. If any of the
"input-files" are directories, the contents of those directories are
added to the JAR archive recursively.
The c and f options can appear in either order, but there must not be
any space between them.

From scratch.. with CMD
The command line is what almost every other application will use to build your JAR file. They just wrap it up a little nicer for you. In truth, it's very simple to do yourself. Obviously Java have explained this way in detail so there is no sense in me repeating it.
Note: You need to have your JDK/bin directoy appended onto your %PATH% system variable to be able to use this method.
Double Note: As pointed out in the comments, I'd suggest you keep trying this method until you understand it. It is very important that you get these things at a low level, so if something goes wrong with the IDE, you have a much better understanding of how to solve it.
Eclipse
Eclipse offers a nice interface for it. You can find a step by step tutorial here. The long and short of it is..
Right click on Project -> Export -> JAR File -> Select the Java files to include
When you've done this, hit finish and you're golden. The tutorial also adds some additional tips in to make it as seamless as possible.
IntelliJ IDEA
My personal favourite IDE. This question offers a nice explanation for how to export as a JAR file. Again, the long and short of it..
File -> Project Structure -> Artifacts
In there, you can then create a new artifact by clicking the + icon. This will give you an option for the file type, which is .JAR and which modules you want to include in your artifact. When you're done, you go to..
Build -> Build Artifacts
And it will create the JAR file from your project.
Using Maven
I've often found this to be a pretty awesome tool, and definitely one worth considering. In IntelliJ, by double clicking the install procedure in the life cycles..
This will create a new JAR file in your .target directory.
Note: Some IDEs (like IntelliJ) will hide the .target directory by default. Make sure you make it visible in the project settings.

A JAR file is nothing but a ZIP file with added meta-information for the Java Runtime Environment. So the easiest way is to actually zip your classes files including that META-INF folder by hand, then rename the file to JAR. How you get your classes files however is a different story.
The easiest practical way is to hit the build button in your IDE, which will then compile your code into class files, create an appropriate set of meta information and then conveniently zips the whole thing into a JAR file.

Asking for a maven solution, you would need to specify a single pom.xml in your project
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>you.specify.something.preferably.a.domain.name</groupId>
<artifactId>name-of-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
And with this you can do a
mvn package
To build the project. However, it relies on a lot of defaults, you would probably want to differ.

To build a jar file from a module
On the main menu, choose Build | Build Artifact.
From the drop-down list, select the desired artifact of the type JAR.
see this

Related

How to make Java package with classes

I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.

How to make Eclipse package ONLY required libraries?

After developing a java app in Eclipse, I would like to deploy it by packaging it into a runnable .jar with only the minimum necessary by the main method and its dependencies.
I have several packages in my workspace I work with too, but do not need to be in the resulting .jar file.
When I try to export, it clearly says that the required libs will be there, but also the other independent packages are inside too. (With the Export option happen exactly the same)
I choose to export only the Main class of the com.project... package, but also the test.project... has been packaged.
In the project I have both com... and test... packages obviously.
How could I force it to truly package only the required ones?
Thanks in advance.
TEMPORARY ANSWER (2019-07-03):
Seems that, for now, there is no way to achieve this automatically, thus the answer from #arnonuem seems a good workaround.
If better news, please feel free to improve this thread.
Thank you all.
I would create an ANT file for this specific task. There you can freely customize which packages should be compiled into the jar and which not.
Please inspire yourself reading this example.
For a general overview what i am talking about you could take a quick look into this.
https://howtodoinjava.com/ant/ant-build-jar-file-example/
Please focus on
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
includes="src/path/to/TheClassToBeIncluded.java" />
For more detailed information on how to include or exclude files or packages you should refer to the documentation over here:
https://ant.apache.org/manual/Tasks/javac.html
At the bottom of this page there is a list with valid build parameters.
First of all, we have to distinguish build and export.
Build transfers .java source code into .class byte code and mostly copies other resources. While doing that it usually merges all source folders into a single bin folder.
Export runs a build (or relies on Build Automatically) and then modifies the resulting .class files and other resources. Usually it packages them into one or more .jar files.
Therefore, our solution includes two steps:
build everything that is unwanted for export into a separate folder (or more)
export from a specific folder (or more)
For step 1, refer to this answer on Stack Overflow.
In a nutshell: Go to Project Properties > Java Build Path > Source tab > Allow output folder for source folders. This enables you to configure a specific output folder for each source folder in the centered viewer.
For step 2, we need to understand that eclipse's Runnable Jar File Export relies on a Launch Configuration.
So before exporting, go to Run > Run Configurations..., select the Classpath tab, remove the default User Entries and hit Advanced.... Now you can Add Folders containing your built classes.
You might want to use separate Run Configurations for internal testing and exporting.
I have struggled with this problem on and off for years, supposing it was just me who was failing to find the right solution. Possible solutions always seem to involve detailed manual configuration e.g. configuration of the build path, or selection of the folders from which classes are exported into a jar, or learning ant, but which still requires manual configuration. The problem is that the inter-dependencies between classes (and packages) are complex - imagine drawing a network diagram from import statements. Manual configuration is time-consuming, error-prone and, I think, infeasible except in simple cases. I am a bit stunned. If there is no automatic solution for selecting necessary classes, I suppose people are regularly exporting their entire code base and that the world is full of bloated jars ... (and, incidentally, without obfuscation, the entire source code base is thereby made available through reverse engineering).

Use a Java file that is in a different place than the rest of the project

I have a java source file in my project, that I want to move to an entirely different location than the rest of the files (my unix home dir), but I still want to be able to properly use it in my project. Is there a way to do this?
EDIT - I see there is a vote to close this for being unclear, so - let's say you have a pJava project in Eclispe. All the source files are neatly saved in their relevant packages, under the same directory. Now, I want to move one of the files to somewhere completely different, but still have it used in my project. I hope this clarifies
Thanks!
I suggest that it depends on what your reason is for moving the source file.
The Java file is still conceptually part of this project, but you're moving it for general organisational purposes. In which case, the new "completely different" directory is another place where sources should be read from, which most IDEs will call a "source root". You should configure your project to read sources from there as appropriate for your IDE.
You're moving the file because it's distinct from this project. In which case I would suggest it ought to be a separate project in its own right. In order to still use the logic in your original project, you'd build the new project into a JAR, and then bring in the JAR as a library dependency (either directly, or using some dependency management system such as Ivy/Maven/etc.). Again, the details will depend on what your current setup is.
Right click on your Eclipse's project -> properties -> java build path , and under the source tab click Link source then choose the parent folder of your java file .

How to use jardesc in Eclipse?

I want to create an executable JAR-file from my eclipse project. It references other projects, which also reference other projects (and so on) and some JAR files.
According to this answer, everything should go "within 2 clicks". But not for me:
It reports a warning:
Problem writing mg/build/classes/META-INF/MANIFEST.MF to JAR: duplicate entry: META-INF/MANIFEST.MF duplicate entry: META-INF/MANIFEST.MF
It looks like it tries to include existing manifests from all projects, which simply doesn't make sense. I actually do not want to include any of them, just to generate a new one. I haven't found any way to switch it off. OK, it's just a warning.
It looks like I need to manually take care of all the referenced jar files... actually, Eclipse knows them, I do not.
I need to either include the content of all the referenced jar files or to copy all of them into the target folder and list them in the Manifest. I've got no idea how to do it.
I also wonder if the jardesc file is usable from ant build scripts.
I got the same error message ("duplicate entry") when, in my case
I checked the option to include an existing manifest file.
AND I specified to include MANIFEST.MF among the files to be included in the .jar.
Once I excluded MANIFEST.MF in the "Select the resources to export:" file tree of the jar generation wizard, the warning disappeared.
As for including referenced projects with your jar, I recommend making them into jar files as well, and including those in the project you want to make a jar of.
Then, make sure to set the manifest's classpath correctly.

Change the package of a JAR from the default package

I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.

Categories