Jump to content

Runnable JAR file

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts
Been trying for 2 days to fix this problem. It'd be awesome if you guys can help. :)

This is just an example code I got online to test to see if I can create a runnable JAR file.

The code is as follows.

package test;


import javax.swing.*;


import java.awt.*;

import java.awt.event.*;

/** 

	This class demonstrates the basics of setting up a Java Swing GUI uisng the

	BorderLayout. You should be able to use this program to drop in other

	components when building a GUI 

*/

public class Willdo {

	// Initialize all swing objects.

    private JFrame f = new JFrame("Basic GUI"); //create Frame

    private JPanel pnlNorth = new JPanel(); // North quadrant 

    private JPanel pnlSouth = new JPanel(); // South quadrant

    private JPanel pnlEast = new JPanel(); // East quadrant

    private JPanel pnlWest = new JPanel(); // West quadrant

    private JPanel pnlCenter = new JPanel(); // Center quadrant


	// Buttons some there is something to put in the panels

    private JButton btnNorth = new JButton("North");

    private JButton btnSouth = new JButton("South");

    private JButton btnEast = new JButton("East");

    private JButton btnWest = new JButton("West");

    private JButton btnCenter = new JButton("Center");


    // Menu

    private JMenuBar mb = new JMenuBar(); // Menubar

    private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar

    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item

    private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry

    private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry


    /** Constructor for the GUI */

    public Willdo(){

		// Set menubar

        f.setJMenuBar(mb);

        

		//Build Menus

        mnuFile.add(mnuItemQuit);  // Create Quit line

        mnuHelp.add(mnuItemAbout); // Create About line

        mb.add(mnuFile);        // Add Menu items to form

        mb.add(mnuHelp);


        // Add Buttons

        pnlNorth.add(btnNorth);

        pnlSouth.add(btnSouth);

        pnlEast.add(btnEast);

        pnlWest.add(btnWest);

        pnlCenter.add(btnCenter);

        

        // Setup Main Frame

        f.getContentPane().setLayout(new BorderLayout());

		f.getContentPane().add(pnlNorth, BorderLayout.NORTH);

		f.getContentPane().add(pnlSouth, BorderLayout.SOUTH);

        f.getContentPane().add(pnlEast, BorderLayout.EAST);

		f.getContentPane().add(pnlWest, BorderLayout.WEST);

        f.getContentPane().add(pnlCenter, BorderLayout.CENTER);

        

		// Allows the Swing App to be closed

        f.addWindowListener(new ListenCloseWdw());

		

		//Add Menu listener

        mnuItemQuit.addActionListener(new ListenMenuQuit());

    }

	

    public class ListenMenuQuit implements ActionListener{

        public void actionPerformed(ActionEvent e){

            System.exit(0);         

        }

    }

	

    public class ListenCloseWdw extends WindowAdapter{

        public void windowClosing(WindowEvent e){

            System.exit(0);         

        }

    }

	

    public void launchFrame(){

        // Display Frame

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.pack(); //Adjusts panel to components for display

        f.setVisible(true);

    }

    

    public static void main(String args[]){

        Willdo gui = new Willdo();

        gui.launchFrame();

    }

}



In Eclipse the program looks like this.

Attached File  89019595.png   19.87K   15 downloads

I'm 100% sure there's nothing wrong with the code.

Oh yea, I'm using Eclipse BTW.

Window #1
Attached File  33348393.png   46.2K   23 downloads

Window #2
Attached File  68321134.png   47.92K   18 downloads



I clicked finish, it's all good.


Now the JAR file's on my desktop.

When I run it. :(

Attached File  49036247.png   50.74K   19 downloads

Please help.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I assume you have the test package in eclipse as well?

Open the .jar file with winrar or 7-zip, or any other zip-program.
In fact, if you change the extension of your jar to .zip windows explorer can even open it.

Once that's open, see if there's a folder in it called "test" with the "WillDo.class" file inside that.

#3
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts

wim DC said:

I assume you have the test package in eclipse as well?

Open the .jar file with winrar or 7-zip, or any other zip-program.
In fact, if you change the extension of your jar to .zip windows explorer can even open it.

Once that's open, see if there's a folder in it called "test" with the "WillDo.class" file inside that.

Yes, it has the WillDo class in it.

wim DC said:

I assume you have the test package in eclipse as well?

Can you explain what you mean by this? I may be doing this wrong. :)

#4
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts
Bump

So, someone help please. ;_;

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Find and extract the manifest.mf file. Open it with notepad, and paste the contents here.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Oh, and don't forget to use CODE tags, or else there would be a mess.

#7
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts
Manifest-Version: 1.0
Class-Path: .
Main-Class: Main_Window


That's all in the file. ^_^

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I believe it should say this for Main-Class:

Main-Class: test.Willdo


#9
Nesan

Nesan

    Newbie

  • Members
  • PipPip
  • 17 posts
No luck, tried it. :(

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Check this out:
Setting an Application's Entry Point (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

From what I can see, the package is called "test" and the class with the main method is Willdo.

Have you tried creating a new project and retest the export?

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is an excuse to install JRE+Netbeans on my PC and learn a bit of packing Java.

I had..
  • Created a file named Willdo.java with your code, and had ran javac on it
  • Placed all generated .class files with a folder named test\
  • Went back up a folder, and created a manifest:
    echo Main-Class: test.Willdo > manifest
    (required ending newline is automatically generated with echo)
  • ran
    jar cfm Willdo manifest [B]test\[/B]*.class
It then runs as it should. For some reason, it will place all classes within the root jar folder instead of the package folder, and will not run so you must place them within test\ yourself before compiling the jar. I had to look at what netbeans generated (of which had worked) to see what they had done differently.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users