+ Reply to Thread
Results 1 to 7 of 7

Thread: Java Programming Tutorialized - Java Package & Executable Jar Files

  1. #1
    Cander's Avatar
    Cander is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Sweden
    Posts
    63
    Rep Power
    0

    Java Programming Tutorialized - Java Package & Executable Jar Files

    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:
    • C:\JavaPackageJarTutorial
    • C:\JavaPackageJarTutorial\TestProject
    • C:\JavaPackageJarTutorial\TestProject\sub
    - Create the following sourcefiles and add its content:
    • manifest.txt located in C:\JavaPackageJarTutorial
    Code:
    Main-Class: TestProject.file1
    (You'll need to make sure this file ends with a new empty line, you'll receive errors otherwise!)
    • 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
    Code:
    package TestProject.sub;
    public class file2
    {
        public String message;
    }
    2. Source compilation

    [For Windows]
    - Open a command prompt window, navigate to C:\JavaPackageJarTutorial and execute the following command to compile the Java files:

    Code:
    javac TestProject\*.java
    3. Creating the executable Jar file

    [For Windows]
    - Execute the following command aswell in C:\JavaPackageJarTutorial to create an executable Jar file of the source:

    Code:
    jar cvfm TestProject.jar manifest.txt TestProject\*.*
    4. Running the application

    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!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    javaman is offline Newbie
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    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?

  4. #3
    javaman is offline Newbie
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    Hi,
    I can succeed with the tutorial.

    When I run the command:
    Code:
    javac TestProject\*.java
    I get 2 errors:

    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();
                                                                       ^
    what might be causing this?

  5. #4
    Cander's Avatar
    Cander is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Sweden
    Posts
    63
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    Quote Originally Posted by javaman View Post
    Hi,
    I can succeed with the tutorial.

    When I run the command:
    Code:
    javac TestProject\*.java
    I get 2 errors:

    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();
                                                                       ^
    what might be causing this?
    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...

  6. #5
    GMVResources's Avatar
    GMVResources is offline Learning Programmer
    Join Date
    Jun 2010
    Posts
    72
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    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!

  7. #6
    Cander's Avatar
    Cander is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Sweden
    Posts
    63
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    Quote Originally Posted by GMVResources View Post
    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!
    Yes, maybe. But IDEs are just messy when they try to help you with everything adding code etc, and I think you lose overview over your project. But that's just me, and I haven't worked with an IDE long enough to be able to learn it properly.

  8. #7
    GMVResources's Avatar
    GMVResources is offline Learning Programmer
    Join Date
    Jun 2010
    Posts
    72
    Rep Power
    0

    Re: Java Programming Tutorialized - Java Package & Executable Jar Files

    That's half true but they tell you your errors like exactly what you need to do =P

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. java I/O BINARY FILES
    By fabiniyoung in forum Java Help
    Replies: 6
    Last Post: 02-27-2011, 06:13 AM
  2. Self Executable Files
    By ryansithe in forum Java Help
    Replies: 5
    Last Post: 06-14-2010, 05:16 AM
  3. wav files in java
    By ido in forum Java Help
    Replies: 0
    Last Post: 03-10-2010, 02:57 PM
  4. Replies: 4
    Last Post: 08-05-2009, 08:32 AM
  5. Java Help Files
    By xXHalfSliceXx in forum Java Help
    Replies: 3
    Last Post: 11-28-2006, 09:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts