75

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

user1227433
  • 875
  • 4
  • 9
  • 9

4 Answers4

131

Create a Java archive (.jar) file using NetBeans as follows:

  1. Right-click on the Project name
  2. Select Properties
  3. Click Packaging
  4. Check Build JAR after Compiling
  5. Check Compress JAR File
  6. Click OK to accept changes
  7. Right-click on a Project name
  8. 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:

  1. Click the Files tab
  2. Expand Project name >> dist

Ensure files aren't being excluded when building the JAR file.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
CBC_NS
  • 1,961
  • 4
  • 27
  • 47
10

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.

user2342262
  • 149
  • 1
  • 4
  • 5
    hey bud the path displays 'dist/JavaApplication1.jar' but I cannot find that in my dist folder – Saumil Dec 25 '13 at 11:54
  • 1
    In the Netbeans IDE you can't see all the files in the 'Projects' (only those the Netbeans programmers think you need for developing), however if you bring up the 'Files' tab you should be able to see all the project files. – John Paul May 06 '15 at 13:54
  • 3
    Netbeans 12 doesn't have a "packaging" option in a project's property dialog. – Tulains Córdova Oct 02 '20 at 09:29
8

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:

  1. Right-click the project's node and choose Properties
  2. Select the Run panel and enter the main class in the Main Class field
  3. Click OK to close the Project Properties dialog box
  4. Clean and build project

Then in the fodler dist the newly created jar should be executable with the command I mentioned above.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Raimundo
  • 605
  • 7
  • 21
  • I'm not seeing a dist folder. Any steps or settings I'm missing? – ch-pub Sep 05 '15 at 04:50
  • Go to the where your NetBeans project is located in the file system. For the folder dist be there you must build or clean and build the project. – Raimundo Sep 06 '15 at 09:02
  • this is good explanation whereas people forget how to running jar file, but its not on the same context as OP asking for. – Yohanim Jul 26 '17 at 03:34
2

Now (2020) NetBeans 11 does it automatically with the "Build" command (right click on the project's name and choose "Build")

seinecle
  • 10,118
  • 14
  • 61
  • 120