how to create jar file in netbeans - java

I want to create independent jar files in netbeans like we can do in eclipse. Normally people suggest to clean and built the project and there is a jar file created. But to run these jar file the .java files, the codes are also required. Isn't there any way to create jar files which runs independently without requiring java codes?
Thanks

set the main class in run configuration
do a clean build
go to the dist folder and you can find the jar there as a runnable one
for further information refer this Creating_an_Executable_Java_File_in_NetBeans

Related

How to create Jar file with external folders and Jars

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

How to make jar file created by netbeans work from any location in the system

I have a java desktop application in netbeans. I have created an executable jar file for the project using clean and build command provided by the netbeans. By using this command the executable jar file gets created under netbeansProjects//dist/.jar. I am able to execute this jar file from command line using java -jar .jar from within project path. But the problem is that when i move this jar outside of netbeans projects folder, say to desktop and run the jar file, it is giving error of type "Exception in thread "main" java.lang.NoClassDefFoundError". How to solve this problem and make the jar file executable from any location of the system.
Complete instructions may be found in dist/README.TXT:
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Ensure that the manifest inside of the jar file contains the necessary classpaths. If you are unfamiliar with the concept, go here: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Netbeans has probably included any external projects/libraries/Jars in the dist/lib folder.
In order to run the application, you must include all the files in the dist folder when you copy the application
Check if in your projects Manifest.mf file has the Attribute
"Main-Class" set to your projects current main Class file.

Running my java project from a Jar

I have a Java project that I'm working on in Eclipse. I have all my code in a src. Eclipse automatically compiles my .java files and stores the .class files in a directory called tmp.
Earlier, I was suing Ant to run my project. My ant script would would compile my Java code, and Jar all the .class files into a .jar and store it into a jar directory. In this jar directory I have some other files e.g. settings.cfg and data.lst. My application needs these files to run but they should not be included in the Jar. They will be deployed along with my JAR and should reside in the same directory. Currently while developing, I have modified and customised my settings and properties files to contain additional information. I don't want these files replaced every time I have a new build as I would like the data to persist across builds. I'm just looking to get the built JAR into the jar directory and execute that. The rest of the files stay the same.
How can I configure my Eclipse so that it always compiles my code, JARs it to the jar directory and executes that JAR. That way, my "extra files" are always in the same place as the JAR.
I'm having a lot of trouble figuring out how to accomplish this.
If you need this feature for fast running/debuging, create new Run Configuration and in the Arguments tab, Working Directory specify the "jar" directory.
The answer was found on another SO post. It was about creating a new "Run Configuration", removing the default classpath entries and adding a the folder containing the JAR i.e. the jar directory and also the JAR file itself.
Here: https://stackoverflow.com/a/1732316/304151
I also had to modify the build configuration a bit. For automatic builds, it used the Java Builder to compile all the .java files into .class files and store it in the tmp directory. I used a custom Ant task to package all the .class files from the tmp directory into a JAR and store it in the jar directory. This is the file that Eclipse executes.
In your project, right click Properties, in "Java build path" menu you can set "default output directory" in the Source tab and make it point to jar directory:

Exporting individual packages to a jar - IntelliJ IDEA 10.5.2

Does anyone know how I can export individual packages/class files into a jar with IntelliJ IDEA 10.5.2 instead of exporting the whole project?
Thanks.
Create separate module (right click on the project, then New -> Module) for each packages/classes set you want to be a separate jar.
For anyone who comes here for the answer:
You can make the jar using the command line. All you have to do is, run the command in the parent folder of the package you want to export as jar.
e.g You only want to export "mypackage" which is situated in "project/src/mainpackage/anotherpackage/mypackage"
you go into "anotherpackage" and run the following command.
jar cvf myjar.jar mypackage/
for more information on the jar command, take a look at the documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
NOTE:
if you make a jar of only the source files i.e. .java files inside the src folder, you won't be able to use it as a library in other projects.
Be sure to make a jar of the .class files(i.e. compiled files) for such use.
You can quickly export the jar package you want to export by installing the Handy Export Jar plug-in.

I need some help concerning using 2 jar files in Java

Well I have 2 .jar files. The main jar file is the jar file for my whole project and the other .jar file being the MySql JDBC Connector.
Well basically whats happening right now is that when I build the project I have the one main .jar file with everything but the MySql JDBC Connector .jar file is inside the main jar file when it builds in NetBeans.
Now when I am just running the project from within NetBeans the MySql JDBC driver can be found inside the src/com/game/mysql folder that I have it in. But when I build the project the Java application cannot locate the JDBC driver from within the main jar file.
When I open the main jar file with WinRar I can see that the JDBC jar file is still in its /com/game/mysql/ folder. But why cant the Java application access it?
I have heard that nested .jar files are not supported in Java so Im thinking this might be the reason although Im not sure if thats true. Is there a way that I can make it so that the application can find the JDBC .jar file within the main jar file?
Also I have done the thing in NetBeans where you add the .jar file through right clicking project -> properties -> Library -> Add Folder/Jar. Thats what makes it work in the NetBeans run but still not the App build.
I have heard that nested .jar files are not supported in Java
More precise, classes in a JAR file which is packaged as a child JAR inside a main JAR are indeed by default invisible to classes in the main JAR.
You have basically 2 options:
Ship your application with 2 loose JARs: your.jar and mysql.jar and define the relative path to the mysql.jar in the Class-Path entry of the MANIFEST.MF file of your.jar.
Class-Path: mysql.jar
When you put both JARs in the same folder and execute your.jar by java -jar your.jar, then it will work.
Let your IDE repackage all loose classes of mysql.jar inside your.jar or add a special classloader which preloads the classes of any embedded JARs. Since I don't do Netbeans, I can't tell whether it supports it and if so, how to do it. In Eclipse, however, this is definitely possible. See also this answer.

Categories