Java creating .jar file
In order to create a .jar file, you need to use jar
instead of java
:
jar cf myJar.jar myClass.class
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)
) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class
header (e.g., Main-Class: myClass
).
However, as Mark Peters pointed out, with JDK 6, you can use the e
option to define the entry point:
jar cfe myJar.jar myClass myClass.class
Finally, you can execute it:
java -jar myJar.jar
See also
- Creating a JAR File
- Setting an Application's Entry Point with the JAR Tool
How to create jar file with package structure?
You need to start creating the JAR at the root of the files.
So, for instance:
jar cvf program.jar -C path/to/classes .
That assumes that path/to/classes
contains the com
directory.
FYI, these days it is relatively uncommon for most people to use the jar
command directly, as they will use a build tool such as Ant or Maven to take care of that (and other aspects of the build). It is well worth the effort of allowing one of those tools to take care of all aspects of your build, and it's even easier with a good IDE to help write the build.xml
(Ant) or pom.xml
(Maven).
Create jar including classes and jar files
Create a folder, unpack mysql-connector-java-5.0.8-bin.jar in it, add deduction.java and run
jar cvf deduction.jar *
inside this folder. Or copy mysql-connector-java-5.0.8-bin.jar as deduction.jar then add deduction.java to it
jar uvf deduction.jar deduction.java
The simplest way to create jar file?
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.
how to create a java jar file with source code (.java files)?
Creating a jar file using IntelliJ
For Intellij IDEA version 11.0.2
File | Project Structure | Artifacts then you should press alt+insert or click the plus icon and create new artifact choose --> jar --> From modules with dependencies.
Next goto Build | Build artifacts --> choose your artifact.
Refer this link create-jar-with-IntelliJ
Creating a jar File in Command Prompt
- Start Command Prompt.
- Navigate to the folder that holds your class files:
C:\>cd \mywork
- Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%
- Compile your class(es):
C:\mywork> javac *.java
- Create a manifest file
manifest.txt with, for example, this content:
Manifest-Version: 1.0
Created-By: 1.8.0_171 (Oracle Corporation) #your jdk version
Main-Class: mainPackage.MainClass
- Create the jar file:
C:\mywork> jar cvfm Craps.jar manifest.txt *.class
How can I create a jar file with an entry point inside a file tree?
See this tutorial. The argument for the jar
command's e
option should be a class name, not a path name. Furthermore, when specifying the list of classes to be included in the jar, care should be taken to reference the files such that the directory structure matches the package name. Otherwise the classpath lookups will fail. Try:
jar -cef a.MainClass formatter.jar -C bin .
Java compiler not creating .jar files
Rest assured: there are no silent errors.
When a tool like javac
fails it will print some error message to the standard "error" console.
Thing is:
javac Main.java
will simply create a file Main.class
within the same directory.
Which you could try to run using java Main
.
That is all there is to this. Well, see here for all the glory details of javac.
Related Topics
Java Apns Certificate Error with "Derinputstream.Getlength(): Lengthtag=109, Too Big."
How Is String Concatenation Implemented in Java 9
Can't Install Rjava on Ubuntu System
Unable to Compile Jni Program Rjava
Simple Program to Call R from Java Using Eclipse and Rserve
Android Color Notification Icon
How to Set -Source 1.7 in Android Studio and Gradle
Spring Boot - Handle to Hibernate Sessionfactory
Reading Properties File in Java
How Does One Configure Rjava on Osx to Select the Right Jvm -- .Jinit() Failing
Environment Variables in Eclipse
Java: Literal Percent Sign in Printf Statement
How to Match Nested Brackets with a Regex Without Using Recursion or Balancing Groups
How to Calculate the Pixel Width of a String in Javafx