Java Programming Tutorialized
This is a Java programming tutorial in the "Java Programming Tutorialized" series
Java Package & Executable Jar Files
Author: Cander
Published: 2010/03/04
Java version: JDK 1.6 u17
Level of difficulty: Easy
This was first a little documentation about the Java Package system and executable Jar files I wrote for myself after solving a problem I recently experienced, but which I later on decided to remake into a tutorial in the series due to its content felt worthy enough to share. I hope you get use of it if you also are experiencing problems concerning this or why not using it like a template for structing your own projects. Using the Java package system is a good habit because it make your projects more structured and knowing how to create executable Jar files is handy when your about to publish your finished project.
In this tutorial it's shown how to create a simple working application structured using the Java Package system, and after that putting the application source together into an executable Jar file. I'm not explaning very much how and why here, and I'm therefore referring to other tutorials on the subject if this wasn't what you were after.
Let's get started:
1. Setting up directories & source
- Create the following directories:- Create the following sourcefiles and add its content:
- C:\JavaPackageJarTutorial
- C:\JavaPackageJarTutorial\TestProject
- C:\JavaPackageJarTutorial\TestProject\sub
- manifest.txt located in C:\JavaPackageJarTutorial
(You'll need to make sure this file ends with a new empty line, you'll receive errors otherwise!)Code:Main-Class: TestProject.file1
- file1.java located in C:\JavaPackageJarTutorial\TestProject
Code:package TestProject; public class file1 { public static void main(String[] arguments) { TestProject.sub.file2 testObject = new TestProject.sub.file2(); testObject.message = "TestProject is a success!"; javax.swing.JOptionPane.showMessageDialog(null, testObject.message, "Java Programming Tutorialized", javax.swing.JOptionPane.INFORMATION_MESSAGE); } }
- file2.java located in C:\JavaPackageJarTutorial\TestProject\sub
2. Source compilationCode:package TestProject.sub; public class file2 { public String message; }
[For Windows]
- Open a command prompt window, navigate to C:\JavaPackageJarTutorial and execute the following command to compile the Java files:
3. Creating the executable Jar fileCode:javac TestProject\*.java
[For Windows]
- Execute the following command aswell in C:\JavaPackageJarTutorial to create an executable Jar file of the source:
4. Running the applicationCode:jar cvfm TestProject.jar manifest.txt TestProject\*.*
You should now have a Jar file named TestProject.jar located in C:\JavaPackageJarTutorial.
- Run TestProject.jar
5. Summary
When you run TestProject.jar, file1's main method will be executed and therefore a JOptionPane-MessageBox showing up on the screen. You have now successfully created an application with a executable Jar file organized with the Java Package system!
Hi Cander,
I've noted that when using Netbeans, .jar file is created when you compile and run your application. if i want executable jar file of my application, can i just take the same generated jar file, or should i create my own like you did. I mean whats the difference between that jar file created by netbeans and the the one i created manually via the prompt?
Hi,
I can succeed with the tutorial.
When I run the command:
I get 2 errors:Code:javac TestProject\*.java
what might be causing this?Code:TestProject\file1.java:6: package TestProject.sub does not exist TestProject.sub.file2 testObject = new TestProject.sub.file2(); ^ TestProject\file1.java:6: package TestProject.sub does not exist TestProject.sub.file2 testObject = new TestProject.sub.file2(); ^
You must have messed up somewhere with the directories or files, retry following the tutorial doing exactly as it tells you to.
PS: I didn't use Netbeans while doing this tutorial, just the simple text editor SciTE and the command prompt for executing the commands, which could maybe somehow causing the problem...
Cander I think you should use an IDE instead of a text editor and I use NetBeans but there shouldn't be an error. Overall, nice job!
That's half true but they tell you your errors like exactly what you need to do =P
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks