How to create an artifact jar using the command line only? - java

I have a java project and using IntelliJ in project settings I've created an Artifact JAR. My project is of the form:
lib/MyInterface.jar
src/com/test/MyTest.java (does not contain a main method)
Where MyTest.java references MyInterface.jar.
When I run [Build -> Build Artifacts -> MyTest.jar -> Build] IntelliJ creates a jar that simply contains MyInterface.class and MyTest.class. I'm using this JAR as a library in another project.
What I need is to be able to create this JAR without having to use IntelliJ or Maven or anything other than javac and jar (due to constraints on the machine this code will be deployed to). Is this possible and if so how can I go about doing this using the command line only?

jar cf ${name for the created jar} input-file(s)
or cd into your package directory then use:
jar cf ${name for the created jar} *

Related

Java executable from NetBeans [duplicate]

Well I have my source code that i have done using the IDE netbeans. Now I wanted to move this java application to a web application. For that I need to create a jar file from my source code, so that I could invoke it in ma jsp file.
I have not been able to find any option in netbeans or any other way to create a .jar file of this source code.
Could someone tell me how to do that.
Thanks
Create a Java archive (.jar) file using NetBeans as follows:
Right-click on the Project name
Select Properties
Click Packaging
Check Build JAR after Compiling
Check Compress JAR File
Click OK to accept changes
Right-click on a Project name
Select Build or Clean and Build
Clean and Build will first delete build artifacts (such as .class files), whereas Build will retain any existing .class files, creating new versions necessary. To elucidate, imagine a project with two classes, A and B.
When built the first time, the IDE creates A.class and B.class. Now you delete B.java but don't clear out B.class. Executing Build should leave B.class in the build directory, and bundle it into the JAR. Selecting Clean and Build will delete B.class. Since B.java was deleted, no longer will B.class be bundled.
The JAR file is built. To view it inside NetBeans:
Click the Files tab
Expand Project name >> dist
Ensure files aren't being excluded when building the JAR file.
Please do right click on the project and go to properties.
Then go to Build and Packaging.
You can see the JAR file location that is produced by defualt setting of netbean in the dist directory.
I also tried to make an executable jar file that I could run with the following command:
java -jar <jarfile>
After some searching I found the following link:
Packaging and Deploying Desktop Java Applications
I set the project's main class:
Right-click the project's node and choose Properties
Select the Run panel and enter the main class in the Main Class field
Click OK to close the Project Properties dialog box
Clean and build project
Then in the fodler dist the newly created jar should be executable with the command I mentioned above.
Now (2020) NetBeans 11 does it automatically with the "Build" command (right click on the project's name and choose "Build")

How to generate jar file from spring boot application using gradle?

I am creating a spring boot application using gradle. I need to create
a jar which include all needed libraries for deploying on (aws) ec2
instance.
I am trying to create a jar by...
1)click on project structure
2)then click on Artifact
3)click on + -> jar -> from modules with dependencies -> select project -> select main class
4)click on ok with default settings
5)click on build tab
6)I got a jar file but it doesn't contain classes.
Use Terminal/Command Prompt to generate jar using Gradle
To generate jar without running test cases
./gradlew build -x test
If you want to run the test cases before creating jar, then
./gradlew build
First make sure you have downloaded gradle and set up in your system variable.
to check this simply run gradle -v in your cmd.
Under System Variables select Path, then click Edit. Add an entry for
C:\Gradle\gradle-6.5.1\bin. Click OK to save.
Now you simply goes to project directory and run
./gradlew build -x test
Now you get your executable jar file

How do I create a runnable JAR in IntelliJ as I would in Eclipse

My process for creating a runnable JAR from a project with many libraries with Eclipse has been.
Export > Runnable JAR > Select launch configuration > Package required libraries into generated JAR > Finish
This creates a single JAR in my export destination which I FTP to my server and run fine.
I recently switched to IntelliJ for various reasons and it has been an improvement in all cases except for building my JARs which forces me to go back to eclipse temporarily.
With IntelliJ I:
Open Project Structure > Artifacts and create a new JAR from modules with dependencies. I then have my output layout with the name of the JAR, a META-INF inside it and all my libaries with the format Extracted <lib.jar/> (sorry I can't upload screenshots on this VPN.)
I build my artifact (which is about 15MB bigger) and FTP it to my server, try to run it and I get the error:
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
How do I mimic how I do it in Eclipse?
EDIT
The two JARs that are created differ considerably:
The valid jar that Eclipse creates looks like this:
Whereas the invalid .JAR looks like this:
You're facing two issues one seems major and one is minor:
Major: signature exception
Minor: size of the jar is 15 MB greater than the jar produced by eclipse.
The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar then add all the jar's from the available elements pan as shown in the figure.
It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.
It's probably best to keep the official jar as is and just add it as a
dependency in the manifest file......
It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.
If you are using Maven you need to put META-INF directory under /main/resources folder instead of main/java.
Reference: Intellij - Nikolay Chashnikov
Then you can execute the runnable jar normally with java -jar filename.jar
You can these steps:
1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies
2) Check the Include in project build checkbox.
3) Right mouse click on module -> build module 'ModuleName'
you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.

How to create a jar with external libraries included in Eclipse?

I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
Personally,
None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.
If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,
Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.
If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.
Or
If you have a web project then put all the jars in web-inf/lib folder and do the same step.
Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library. Then try to export as a runnable jar.

How to build a distributable jar with Ant for a java project having external jar dependencies

I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.

Categories

Resources