Results 1 to 7 of 7
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default Creating .Jar files

    Hi, I know this topic has been created in a thread before, but I still can't seem to be able to create JAR files. If someone can please help me with a step-by-step process, or tell me what I'm doing wrong, that would be great.

    I'm creating a simple Slots game, and want to create it as an executable. I have my code in one file "Slots.java" and two classes in this file, "Slots.class" and "Slots$Play.class." The classes are packeged under Package Slots; I also created a manifest file which contains "Main-Class: Slots.Slots." I also added a carriage return after that line. All these files are included in the folder ...bin2/Slots/.

    I try to create the jar file through command prompt. After compiling Slots.java and making sure teh program works, I'm not to sure how to create the jar file.

    Here is the code that I used for Slots.java:

    Java Code:
    package Slots;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class Slots {
    	
    	static JFrame frame;
    	static Container container;
    	static JTextField bet;
    	static JLabel winnings;
    	static JLabel balance;
    	static JLabel slot1;
    	static JLabel slot2;
    	static JLabel slot3;
    	static JButton ride;
    	
    	static double userBalance = 50;
    	static double userBet;
    	static String firstSlot;
    	static String secondSlot;
    	static String thirdSlot;
    	static int one;
    	static int two;
    	static int three;
    	static double temp;
    	
    	static class Play implements ActionListener {
    		
    		public void actionPerformed(ActionEvent ae) {
    			userBet = Double.parseDouble(bet.getText());
    			temp = userBet;
    			RoleSlots();
    			WinorLose();
    			slot1.setText(firstSlot);
    			slot2.setText(secondSlot);
    			slot3.setText(thirdSlot);
    			balance.setText("" + userBalance);
    			winnings.setText("" + temp);
    		}
    		
    		public void RoleSlots() {
    			String outcomes[] = {"BAR", "CHERRY", "7", "BELL"};
    			
    			one = (int)(Math.random() * 3);
    			two = (int)(Math.random() * 3);
    			three = (int)(Math.random() * 3);
    			
    			firstSlot = outcomes[one];
    			secondSlot = outcomes[two];
    			thirdSlot = outcomes[three];
    		}
    		
    		public void WinorLose() {
    			
    			switch(one) {
    			case(0) :
    				switch(two) {
    				case(0) :
    					switch(three) {
    					case(0) :
    						userBalance += userBet * 3;
    						break;
    					}
    				
    				}
    			case(1) :
    				switch(two) {
    				case(1) :
    					switch(three) {
    					case(0) :
    						userBalance += userBet * 0.5;
    						break;
    					case(1) :
    						userBalance += userBet * 1;
    						break;
    					case(2) :
    						userBalance += userBet * 0.5;
    						break;
    					case(3) :
    						userBalance += userBet * 0.5;
    						break;
    					}
    				}
    			case(2) :
    				switch(two) {
    				case(2) :
    					switch(three) {
    					case(2) :
    						userBalance += userBet * 4;
    						break;
    					}
    				}
    			case(3) :
    				switch(two) {
    				case(3) :
    					switch(three) {
    					case(0) :
    						userBalance += userBet * 1;
    						break;
    					case(1) :
    						userBalance += userBet * 1;
    						break;
    					case(2) :
    						userBalance += userBet * 1;
    						break;
    					case(3) :
    						userBalance += userBet * 2;
    					}
    				}
    				default : userBalance -= userBet;
    			}
    		}
    	}
    	public static void main(String[] args) {
    		
    		frame = new JFrame();
    		container = new Container();
    		bet = new JTextField();
    		winnings = new JLabel();
    		balance = new JLabel("Balance Shown Here");
    		slot1 = new JLabel();
    		slot2 = new JLabel();
    		slot3 = new JLabel();
    		ride = new JButton("Ride");
    		Play p = new Play();
    		
    		ride.addActionListener(p);
    		frame.setSize(500,500);
    		container = frame.getContentPane();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		container.setLayout(new GridLayout(3,3));
    		container.add(bet);
    		container.add(winnings);
    		container.add(balance);
    		container.add(slot1);
    		container.add(slot2);
    		container.add(slot3);
    		container.add(ride);
    		frame.setVisible(true);
    	}
    	
    }
    Here is the content of mainClass (The manifest file):

    Main-Class: Slots.Slots

    I've tried using in command prompt "jar cmf mainClass Slots.jar Slots.class Slots

    All files are located in ...java/jdk1.6.0_02/bin2/Slots/. I also tried various variations for jar, and I'm still unable to create a working jar file. Someone please help me. Thanks

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    i do it a different way. The way you're doing it is probably the better way. But mine, you don't need to create a manifest file. You just have to specify the main class. For example.

    If i had two classes ExampleMain (who contains main()) and Example
    Here is how i would do it at the command line

    Java Code:
    jar cfe MyJar.jar ExampleMain ExampleMain.class Example.class
    the segment: ExampleMain tells it which class contains main().

    Let me know how this works out for you.

  3. #3
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default

    Ok, This is what I typed in:

    jar cfe SlotsGame.jar Slots Slots.class Slots$Play.class

    This creates a Jar file. Thanks for helping me get past that part. Now what can I do with the jar file. If I execute the jar file, should I be able to run the program??
    Jai guru deva om, Nothing's gonna change my world

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Well that really depends. If it is a console application (uses System.out.println() and Scanners), it cannot be executed via double-click. However, it can be executed by the following command, in a DOS window.

    Java Code:
    java -jar SlotsGame.jar
    Now, on the other hand, if it is graphical, you can run it via double-click, and of course you can use the above command.

    Oh and I'm glad i could help you create your jar.

    Here's something that i thought was a really cool idea. I made a program which makes jars for the user (after they specify a few things). The way it does it, is that it creates a MS-DOS batch file which executes the commands i showed you, thus created a .jar file. It's pretty cool, if anyone would like me to send them the finished program (which is a jar program =]), I'd be glad to. (i just gotta finish up a few things, like a user manual)

  5. #5
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default

    Does having JButtons and a JFrame count as having a graphical program? Because when I run the .jar executable file, nothing happens.
    Jai guru deva om, Nothing's gonna change my world

  6. #6
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Hmm... I'm actually not too sure on that. Anyone else know more information on this?

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Console or window - don't be judgmental.

    Well, I have put up a Window - a true, actual graphical window as we all know it - from a console program that was actually already running. Whether one could get a console window to popup from a True Graphical, I neither know nor care. I design my programs with the idea that anything the user can see or point at in the routine use of the computer counts as User Interface, thus an actual file created by a running program counts ~ generally ~ in the broadest scope as a user interface.

    Having javax.swing.anything makes the program 'graphical' if we want to call it that. Writing only to System.out and to files created by the program generally puts it in the Console Program category. Either way, making a jar file that the intended users can run as a program is done by having one class that has a public static void main(String[].... and that is invoked by the Java Runtime, aka jvm or Java Virtual Machine. It may be that graphical ( true gui programs that are neither Console nor silent utilities ) are runnable by javaw without the console window hanging around on the screen.

    We do however need an investment of some effort on the part of the user.

    We have a file named .jar in which there is a directory named mainfest-mf or something.

    In that directory there is a file called mainfest.mf ~ you open it and add the lines :
    Java Code:
    Main-Class: (the name of your class that has the main() method)
    OR

    Java Code:
    Main-Class: pack.ClassName
    Where pack.ClassName is your package name and main() class name.

    Even with that, the intended user has to be able to understand your directions given.

    See: Notes about the Java Programming Language

    by:
    Ryan Stansifer
    Last modified: Tue Feb 20 14:37:17 EST 2007

Similar Threads

  1. Creating ZIP files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:14 PM

Posting Permissions

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