Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Program Breaks When In Jar Format

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Program Breaks When In Jar Format

    Hey, I just read up on how to turn a small program that I've finished into a .Jar file so it can be run without the cmd. I followed two different tutorials that both had the same information and the same steps, the Jar is created successfully and when you run it the GUI comes up just as it should but when you try to get the program to do anything it doesn't work.

    For example; When I enter the numbers into the attack formula and hit the calculate button it should show the calculated attack damage in the attack output area but nothing changes. When I do the same thing while running the .class file from the cmd it works perfectly.

    I'm not sure what's going on since I know the program works in .class but for some reason it's not working in .jar.


    Program:
    import javax.swing.*;
    import java.awt.event.*; //Needed for action event listeners for the buttons
     
    class DnDFormulator extends JFrame
    {
    	public static boolean criticalStrikeTrueOrFalse;
     
    	int MA = 0; //Main Attribute - Attack Formula
    	int WR = 0; //Weapon Roll - Attack Formula
    	int PBA = 0; //Passive Bonus - Attack Formula
    	int BD = 0; //Base Damage - Attack Formula
    	int CON = 0; //Constitution - Defence Formula
    	int ARM = 0; //Armor - Defense Formula
    	int PBD = 0; //Passive Bonus - Defense Formula
    	int SD = 0; //Spell Damage - Magic Formula
    	int INT = 0; //Intellect - Magic Formula
    	int PBM = 0; //Passive Bonus - Magic Formula
    	int SP = 0; //Spell power - Magic Formula
    	int HP = 0; //Healing Power = Magic Frmula
    	public static double AFO = 0.0; //Attack Formula Output
    	public static double DFO = 0.0; //Defence Formula Output
    	public static double MFO = 0.0; //Magic Formula Output
     
    	public static JTextArea magicFormulaSpellDamage = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea magicFormulaIntellect = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea magicFormulaPassiveBonus = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea magicFormulaSpellPower = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea magicFormulaHealingPower = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public static JTextArea attackFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea defenceFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public static JTextArea magicFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public static JLabel attackFormulaLabel1 = new JLabel("Main Attribute");
    	public static JLabel attackFormulaLabel2 = new JLabel("Weapon Roll");
    	public static JLabel attackFormulaLabel3 = new JLabel("Passive Bonus");
    	public static JLabel attackFormulaLabel4 = new JLabel("Base Damage");
    	public static JLabel defenceFormulaLabel1 = new JLabel("Constitution");
    	public static JLabel defenceFormulaLabel2 = new JLabel("Armor");
    	public static JLabel defenceFormulaLabel3 = new JLabel("Passive Bonus");
    	public static JLabel criticalStrikeLabel1 = new JLabel("Crit?");
    	public static JLabel magicFormulaLabel1 = new JLabel("Spell Damage");
    	public static JLabel magicFormulaLabel2 = new JLabel("Intellect");
    	public static JLabel magicFormulaLabel3 = new JLabel("Passive Bonus");
    	public static JLabel magicFormulaLabel4 = new JLabel("Spell Power");
    	public static JLabel magicFormulaLabel5 = new JLabel("Healing Power");
    	public static JLabel attackFormulaOutputLabel = new JLabel("A Output");
    	public static JLabel defenceFormulaOutputLabel = new JLabel("D Output");
    	public static JLabel magicFormulaOutputLabel = new JLabel("M Output");
    	public static JLabel attackFormulaLineLabel = new JLabel("AF");
    	public static JLabel defenceFormulaLineLabel = new JLabel("DF");
    	public static JLabel magicFormulaLineLabel = new JLabel("MF");
     
    	public static JTextArea attackFormulaMainAttribute = new JTextArea("", 1, 4); //String, Rows, Columns
    	public static JTextArea attackFormulaWeaponRoll = new JTextArea("", 1, 4); //String, Rows, Columns
    	public static JTextArea attackFormulaPassiveBonus = new JTextArea("", 1, 4); //String, Rows, Columns
    	public static JTextArea attackFormulaBaseDamage = new JTextArea("", 1, 4); //String, Rows, Columns
     
    	public static JTextArea defenceFormulaConstitution = new JTextArea("", 1, 4); //String, Rows, Columns
    	public static JTextArea defenceFormulaArmor = new JTextArea("", 1, 4); //String, Rows, Columns
    	public static JTextArea defenceFormulaPassiveBonus = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public static JRadioButton criticalStrikeTrue = new JRadioButton("True"); //Creates a radio button that says True beside it.
    	public static JRadioButton criticalStrikeFalse = new JRadioButton("False"); //Creates a radio button that says False beside it.
     
    	public static void main(String args[])
    	{
    		JFrame frame = new JFrame();
    		JPanel panel = new JPanel();
     
    		frame.setTitle("DnD Formulator");
    		frame.setSize(640, 480); //Sets the size of the game window
    		frame.setLocationRelativeTo(null); //Centers the window on the users screen.
    		frame.setResizable(false); //Makes it so that the user cannot resize the JFrame.
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		frame.getContentPane().add(panel);
    		panel.setLayout(null); //Makes it so you can manually positon everything using XY coords.
     
    		frame.setVisible(true); //Makes the JFrame, also known ass gameWindow visible to the user.
     
    		ButtonGroup criticalButtonGroup = new ButtonGroup(); //Creates a group so that we can group together radio buttons.
    		criticalButtonGroup.add(criticalStrikeTrue); //Adds the criticalStrikeTrue radio button to be added to the criticalButtonGroup.
    		criticalButtonGroup.add(criticalStrikeFalse); //Adds the criticalStrikeFalse radio button to be added to the criticalButtonGroup.
    		criticalStrikeFalse.setSelected(true); //Sets the criticalStrikeFalse radio button to be selected automatically.
     
    		JButton calculateButton = new JButton("Calculate!");
     
    		panel.add(calculateButton);
     
    		panel.add(attackFormulaMainAttribute);
    		panel.add(attackFormulaWeaponRoll);
    		panel.add(attackFormulaPassiveBonus);
    		panel.add(attackFormulaBaseDamage);
    		panel.add(defenceFormulaConstitution);
    		panel.add(defenceFormulaArmor);
    		panel.add(defenceFormulaPassiveBonus);
    		panel.add(criticalStrikeTrue);
    		panel.add(criticalStrikeFalse);
    		panel.add(magicFormulaSpellDamage);
    		panel.add(magicFormulaIntellect);
    		panel.add(magicFormulaPassiveBonus);
    		panel.add(magicFormulaSpellPower);
    		panel.add(magicFormulaHealingPower);
    		panel.add(attackFormulaOutput);
    		panel.add(defenceFormulaOutput);
    		panel.add(magicFormulaOutput);
     
    		panel.add(attackFormulaLabel1);
    		panel.add(attackFormulaLabel2);
    		panel.add(attackFormulaLabel3);
    		panel.add(attackFormulaLabel4);
    		panel.add(defenceFormulaLabel1);
    		panel.add(defenceFormulaLabel2);
    		panel.add(defenceFormulaLabel3);
    		panel.add(criticalStrikeLabel1);
    		panel.add(magicFormulaLabel1);
    		panel.add(magicFormulaLabel2);
    		panel.add(magicFormulaLabel3);
    		panel.add(magicFormulaLabel4);
    		panel.add(magicFormulaLabel5);
    		panel.add(attackFormulaOutputLabel);
    		panel.add(defenceFormulaOutputLabel);
    		panel.add(magicFormulaOutputLabel);
    		panel.add(attackFormulaLineLabel);
    		panel.add(defenceFormulaLineLabel);
    		panel.add(magicFormulaLineLabel);
     
    		attackFormulaMainAttribute.setBounds(50, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaWeaponRoll.setBounds(140, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaPassiveBonus.setBounds(230, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaBaseDamage.setBounds(320, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		defenceFormulaConstitution.setBounds(50, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaArmor.setBounds(140, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaPassiveBonus.setBounds(230, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		criticalStrikeTrue.setBounds(50, 120, 80, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		criticalStrikeFalse.setBounds(130, 120, 80, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		magicFormulaSpellDamage.setBounds(50, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaIntellect.setBounds(140, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaPassiveBonus.setBounds(230, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaSpellPower.setBounds(320, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaHealingPower.setBounds(410, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutput.setBounds(50, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaOutput.setBounds(140, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaOutput.setBounds(230, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
    		defenceFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
    		magicFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
     
    		attackFormulaMainAttribute.setText("0");
    		attackFormulaWeaponRoll.setText("0");
    		attackFormulaPassiveBonus.setText("0");
    		attackFormulaBaseDamage.setText("0");
    		defenceFormulaConstitution.setText("0");
    		defenceFormulaArmor.setText("0");
    		defenceFormulaPassiveBonus.setText("0");
    		magicFormulaSpellDamage.setText("0");
    		magicFormulaIntellect.setText("0");
    		magicFormulaPassiveBonus.setText("0");
    		magicFormulaSpellPower.setText("0");
    		magicFormulaHealingPower.setText("0");
    		attackFormulaOutput.setText("0");
    		defenceFormulaOutput.setText("0");
    		magicFormulaOutput.setText("0");
     
    		attackFormulaLabel1.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel2.setBounds(140, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel3.setBounds(230, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel4.setBounds(320, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		defenceFormulaLabel1.setBounds(50, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLabel2.setBounds(140, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLabel3.setBounds(230, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		criticalStrikeLabel1.setBounds(20, 120, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		magicFormulaLabel1.setBounds(50, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel2.setBounds(140, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel3.setBounds(230, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel4.setBounds(320, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel5.setBounds(410, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutputLabel.setBounds(50, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaOutputLabel.setBounds(140, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaOutputLabel.setBounds(230, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaLineLabel.setBounds(30, 50, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLineLabel.setBounds(30, 90, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLineLabel.setBounds(30, 160, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		calculateButton.setBounds(50, 400, 100, 20);
    		calculateButton.setFocusPainted(false);
    		calculateButton.addActionListener(new ActionListener() {  //Action listener for the newGameButton
    			public void actionPerformed(ActionEvent e) {
    				DnDFormulator DnDFormulator = new DnDFormulator();
    				DnDFormulator.attackCalculationMethod();
    				DnDFormulator.defenceCalculationMethod();
    				DnDFormulator.magicCalculationMethod();
     
    				attackFormulaOutput.setText(""+AFO+"");
    				defenceFormulaOutput.setText(""+DFO+"");
    				magicFormulaOutput.setText(""+MFO+"");
    			}});
    	}
     
    	public double attackCalculationMethod()
    	{
    		String AF1 = attackFormulaMainAttribute.getText();
    		String AF2 = attackFormulaWeaponRoll.getText();
    		String AF3 = attackFormulaPassiveBonus.getText();
    		String AF4 = attackFormulaBaseDamage.getText();
     
    		MA = Integer.parseInt(AF1);
    		WR = Integer.parseInt(AF2);
    		PBA =  Integer.parseInt(AF3);
    		BD =  Integer.parseInt(AF4);
     
    		if ( criticalStrikeTrue.isSelected() )
    			AFO = ( ( ( MA / 2 ) + WR + PBA + BD ) * 2 );
    		else
    			AFO = ( ( MA / 2 ) + WR + PBA + BD);
     
    		return AFO;
    	}
     
    	public double defenceCalculationMethod()
    	{
    		String DF1 = defenceFormulaConstitution.getText();
    		String DF2 = defenceFormulaArmor.getText();
    		String DF3 = defenceFormulaPassiveBonus.getText();
     
    		CON =  Integer.parseInt(DF1);
    		ARM =  Integer.parseInt(DF2);
    		PBD =  Integer.parseInt(DF3);
     
    		criticalStrikeTrueOrFalse = criticalStrikeTrue.isSelected();
     
    		if ( criticalStrikeTrue.isSelected() )
    			DFO = ( ( (CON / 3) + ARM + PBD ) * 2 );
    		else 
    			DFO = ( (CON / 3) + ARM + PBD );
     
    		return DFO;
    	}
     
    	public double magicCalculationMethod()
    	{
    		String MF1 = magicFormulaSpellDamage.getText();
    		String MF2 = magicFormulaIntellect.getText();
    		String MF3 = magicFormulaPassiveBonus.getText();
    		String MF4 = magicFormulaSpellPower.getText();
    		String MF5 = magicFormulaHealingPower.getText();
     
    		SD =  Integer.parseInt(MF1);
    		INT =  Integer.parseInt(MF2);
    		PBM = Integer.parseInt(MF3);
    		SP =  Integer.parseInt(MF4);
    		HP =  Integer.parseInt(MF5);
    		int SHP = 1;
     
    		if ( SP == 0 )
    			SHP = HP;
    		else
    			SHP = SP;
     
    		if ( PBM == 0)
    			PBM = 1;
     
    		criticalStrikeTrueOrFalse = criticalStrikeTrue.isSelected();
     
    		if ( criticalStrikeTrue.isSelected() )
    			MFO = ( ( ( ( SD + ( INT / 2 ) ) * PBM ) * ( SHP / 2 ) ) * 2 );
    		else
    			MFO = ( ( ( SD + ( INT / 2 ) ) * PBM ) * ( SHP / 2 ) );
     
    		return MFO;
    	}
    }


    Manifest File:
    Main-Class: DnDFormulator


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program Breaks When In Jar Format

    Have you run your jar via the command line (java -jar MyJar.jar)? I'd bet one or more exceptions are thrown, and doing so via the command line will should the exceptions (or rule out this possibility)

  3. The Following User Says Thank You to copeg For This Useful Post:

    tyeeeee1 (December 9th, 2012)

  4. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Program Breaks When In Jar Format

    When I run into problems like this, I try to pare my code down, to simplify it as much as possible so that it is as small as can be, yet still show the problem. This way I can isolate the error and be better able to see it and fix it. Also consider using some type of logging so as to output program state as the program runs.

    A few un-asked for criticisms of your code:
    • Almost everything static should be non-static.
    • You really shouldn't create a new DnDFormulator object to do your calculations. Rather do them using the current instance.
    • The methods that are for calculating values should return those values rather than placing them into a static field.
    • You will want to have your code follow Java coding convention rules, meaning variable and method names should all begin with lower-case letters, class names, enum names, and the like should begin with upper-case letter, constants should be all-capitalized. This is especially important when you are asking others, volunteers, to help you with your code. Since you're asking them to put in volunteer effort to help you, we should expect a similar effort back from you so as to make your code easier for us to understand and correct.


    Luck!

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    tyeeeee1 (December 9th, 2012)

  6. #4
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Program Breaks When In Jar Format

    @curmudgeon

    Thanks for the criticism, I've tried to chance the code based on what you've said but I ran into one problem and one question.
    The public variables of the DnDFormulator class, when changed to non-static, throw up a wall of "Non-Static variable variableName cannot be referenced from a static context."; That is why I had them all set to static.
    As for the question, I'm not entirely sure what you're speaking of about the static fields and that.




    Edit:

    Updated code:
    import javax.swing.*;
    import java.awt.event.*; //Needed for action event listeners for the buttons
     
    class DnDFormulator extends JFrame
    {
    	public static boolean criticalStrikeTrueOrFalse;
     
    	int mA = 0; //Main Attribute - Attack Formula
    	int wR = 0; //Weapon Roll - Attack Formula
    	int pBA = 0; //Passive Bonus - Attack Formula
    	int bD = 0; //Base Damage - Attack Formula
    	int cON = 0; //Constitution - Defence Formula
    	int aRM = 0; //Armor - Defense Formula
    	int pBD = 0; //Passive Bonus - Defense Formula
    	int sD = 0; //Spell Damage - Magic Formula
    	int iNT = 0; //Intellect - Magic Formula
    	int pBM = 0; //Passive Bonus - Magic Formula
    	int sP = 0; //Spell power - Magic Formula
    	int hP = 0; //Healing Power = Magic Frmula
    	public double aFO = 0.0; //Attack Formula Output
    	public double dFO = 0.0; //Defence Formula Output
    	public double mFO = 0.0; //Magic Formula Output
     
    	public JTextArea magicFormulaSpellDamage = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea magicFormulaIntellect = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea magicFormulaPassiveBonus = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea magicFormulaSpellPower = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea magicFormulaHealingPower = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public JTextArea attackFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea defenceFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
    	public JTextArea magicFormulaOutput = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public JLabel attackFormulaLabel1 = new JLabel("Main Attribute");
    	public JLabel attackFormulaLabel2 = new JLabel("Weapon Roll");
    	public JLabel attackFormulaLabel3 = new JLabel("Passive Bonus");
    	public JLabel attackFormulaLabel4 = new JLabel("Base Damage");
    	public JLabel defenceFormulaLabel1 = new JLabel("Constitution");
    	public JLabel defenceFormulaLabel2 = new JLabel("Armor");
    	public JLabel defenceFormulaLabel3 = new JLabel("Passive Bonus");
    	public JLabel criticalStrikeLabel1 = new JLabel("Crit?");
    	public JLabel magicFormulaLabel1 = new JLabel("Spell Damage");
    	public JLabel magicFormulaLabel2 = new JLabel("Intellect");
    	public JLabel magicFormulaLabel3 = new JLabel("Passive Bonus");
    	public JLabel magicFormulaLabel4 = new JLabel("Spell Power");
    	public JLabel magicFormulaLabel5 = new JLabel("Healing Power");
    	public JLabel attackFormulaOutputLabel = new JLabel("A Output");
    	public JLabel defenceFormulaOutputLabel = new JLabel("D Output");
    	public JLabel magicFormulaOutputLabel = new JLabel("M Output");
    	public JLabel attackFormulaLineLabel = new JLabel("AF");
    	public JLabel defenceFormulaLineLabel = new JLabel("DF");
    	public JLabel magicFormulaLineLabel = new JLabel("MF");
     
    	public JTextArea attackFormulaMainAttribute = new JTextArea("", 1, 4); //String, Rows, Columns
    	public JTextArea attackFormulaWeaponRoll = new JTextArea("", 1, 4); //String, Rows, Columns
    	public JTextArea attackFormulaPassiveBonus = new JTextArea("", 1, 4); //String, Rows, Columns
    	public JTextArea attackFormulaBaseDamage = new JTextArea("", 1, 4); //String, Rows, Columns
     
    	public JTextArea defenceFormulaConstitution = new JTextArea("", 1, 4); //String, Rows, Columns
    	public JTextArea defenceFormulaArmor = new JTextArea("", 1, 4); //String, Rows, Columns
    	public JTextArea defenceFormulaPassiveBonus = new JTextArea("", 1, 4);  //String, Rows, Columns
     
    	public JRadioButton criticalStrikeTrue = new JRadioButton("True"); //Creates a radio button that says True beside it.
    	public JRadioButton criticalStrikeFalse = new JRadioButton("False"); //Creates a radio button that says False beside it.
     
    	public static void main(String args[])
    	{
    		JFrame frame = new JFrame();
    		JPanel panel = new JPanel();
     
    		frame.setTitle("DnD Formulator");
    		frame.setSize(640, 480); //Sets the size of the game window
    		frame.setLocationRelativeTo(null); //Centers the window on the users screen.
    		frame.setResizable(false); //Makes it so that the user cannot resize the JFrame.
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		frame.getContentPane().add(panel);
    		panel.setLayout(null); //Makes it so you can manually positon everything using XY coords.
     
    		frame.setVisible(true); //Makes the JFrame, also known ass gameWindow visible to the user.
     
    		ButtonGroup criticalButtonGroup = new ButtonGroup(); //Creates a group so that we can group together radio buttons.
    		criticalButtonGroup.add(criticalStrikeTrue); //Adds the criticalStrikeTrue radio button to be added to the criticalButtonGroup.
    		criticalButtonGroup.add(criticalStrikeFalse); //Adds the criticalStrikeFalse radio button to be added to the criticalButtonGroup.
    		criticalStrikeFalse.setSelected(true); //Sets the criticalStrikeFalse radio button to be selected automatically.
     
    		JButton calculateButton = new JButton("Calculate!");
     
    		panel.add(calculateButton);
     
    		panel.add(attackFormulaMainAttribute);
    		panel.add(attackFormulaWeaponRoll);
    		panel.add(attackFormulaPassiveBonus);
    		panel.add(attackFormulaBaseDamage);
    		panel.add(defenceFormulaConstitution);
    		panel.add(defenceFormulaArmor);
    		panel.add(defenceFormulaPassiveBonus);
    		panel.add(criticalStrikeTrue);
    		panel.add(criticalStrikeFalse);
    		panel.add(magicFormulaSpellDamage);
    		panel.add(magicFormulaIntellect);
    		panel.add(magicFormulaPassiveBonus);
    		panel.add(magicFormulaSpellPower);
    		panel.add(magicFormulaHealingPower);
    		panel.add(attackFormulaOutput);
    		panel.add(defenceFormulaOutput);
    		panel.add(magicFormulaOutput);
     
    		panel.add(attackFormulaLabel1);
    		panel.add(attackFormulaLabel2);
    		panel.add(attackFormulaLabel3);
    		panel.add(attackFormulaLabel4);
    		panel.add(defenceFormulaLabel1);
    		panel.add(defenceFormulaLabel2);
    		panel.add(defenceFormulaLabel3);
    		panel.add(criticalStrikeLabel1);
    		panel.add(magicFormulaLabel1);
    		panel.add(magicFormulaLabel2);
    		panel.add(magicFormulaLabel3);
    		panel.add(magicFormulaLabel4);
    		panel.add(magicFormulaLabel5);
    		panel.add(attackFormulaOutputLabel);
    		panel.add(defenceFormulaOutputLabel);
    		panel.add(magicFormulaOutputLabel);
    		panel.add(attackFormulaLineLabel);
    		panel.add(defenceFormulaLineLabel);
    		panel.add(magicFormulaLineLabel);
     
    		attackFormulaMainAttribute.setBounds(50, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaWeaponRoll.setBounds(140, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaPassiveBonus.setBounds(230, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaBaseDamage.setBounds(320, 50, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		defenceFormulaConstitution.setBounds(50, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaArmor.setBounds(140, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaPassiveBonus.setBounds(230, 90, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		criticalStrikeTrue.setBounds(50, 120, 80, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		criticalStrikeFalse.setBounds(130, 120, 80, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		magicFormulaSpellDamage.setBounds(50, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaIntellect.setBounds(140, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaPassiveBonus.setBounds(230, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaSpellPower.setBounds(320, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaHealingPower.setBounds(410, 160, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutput.setBounds(50, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaOutput.setBounds(140, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaOutput.setBounds(230, 250, 50, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
    		defenceFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
    		magicFormulaOutput.setEditable(false); //Makes it so that the user cannot type in this JTextArea
     
    		attackFormulaMainAttribute.setText("0");
    		attackFormulaWeaponRoll.setText("0");
    		attackFormulaPassiveBonus.setText("0");
    		attackFormulaBaseDamage.setText("0");
    		defenceFormulaConstitution.setText("0");
    		defenceFormulaArmor.setText("0");
    		defenceFormulaPassiveBonus.setText("0");
    		magicFormulaSpellDamage.setText("0");
    		magicFormulaIntellect.setText("0");
    		magicFormulaPassiveBonus.setText("0");
    		magicFormulaSpellPower.setText("0");
    		magicFormulaHealingPower.setText("0");
    		attackFormulaOutput.setText("0");
    		defenceFormulaOutput.setText("0");
    		magicFormulaOutput.setText("0");
     
    		attackFormulaLabel1.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel2.setBounds(140, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel3.setBounds(230, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		attackFormulaLabel4.setBounds(320, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		defenceFormulaLabel1.setBounds(50, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLabel2.setBounds(140, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLabel3.setBounds(230, 70, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		criticalStrikeLabel1.setBounds(20, 120, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		magicFormulaLabel1.setBounds(50, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel2.setBounds(140, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel3.setBounds(230, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel4.setBounds(320, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLabel5.setBounds(410, 140, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaOutputLabel.setBounds(50, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaOutputLabel.setBounds(140, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaOutputLabel.setBounds(230, 230, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		attackFormulaLineLabel.setBounds(30, 50, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		defenceFormulaLineLabel.setBounds(30, 90, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
    		magicFormulaLineLabel.setBounds(30, 160, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     
    		calculateButton.setBounds(50, 400, 100, 20);
    		calculateButton.setFocusPainted(false);
    		calculateButton.addActionListener(new ActionListener() {  //Action listener for the newGameButton
    			public void actionPerformed(ActionEvent e) {
    				DnDFormulator.attackCalculationMethod();
    				DnDFormulator.defenceCalculationMethod();
    				DnDFormulator.magicCalculationMethod();
     
    				attackFormulaOutput.setText(""+aFO+"");
    				defenceFormulaOutput.setText(""+dFO+"");
    				magicFormulaOutput.setText(""+mFO+"");
    			}});
    	}
     
    	public double attackCalculationMethod()
    	{
    		String aF1 = attackFormulaMainAttribute.getText();
    		String aF2 = attackFormulaWeaponRoll.getText();
    		String aF3 = attackFormulaPassiveBonus.getText();
    		String aF4 = attackFormulaBaseDamage.getText();
     
    		mA = Integer.parseInt(aF1);
    		wR = Integer.parseInt(aF2);
    		pBA =  Integer.parseInt(aF3);
    		bD =  Integer.parseInt(aF4);
     
    		if ( criticalStrikeTrue.isSelected() )
    			aFO = ( ( ( mA / 2 ) + wR + pBA + bD ) * 2 );
    		else
    			aFO = ( ( mA / 2 ) + wR + pBA + bD);
     
    		return aFO;
    	}
     
    	public double defenceCalculationMethod()
    	{
    		String dF1 = defenceFormulaConstitution.getText();
    		String dF2 = defenceFormulaArmor.getText();
    		String dF3 = defenceFormulaPassiveBonus.getText();
     
    		cON =  Integer.parseInt(dF1);
    		aRM =  Integer.parseInt(dF2);
    		pBD =  Integer.parseInt(dF3);
     
    		criticalStrikeTrueOrFalse = criticalStrikeTrue.isSelected();
     
    		if ( criticalStrikeTrue.isSelected() )
    			dFO = ( ( (cON / 3) + aRM + pBD ) * 2 );
    		else 
    			dFO = ( (cON / 3) + aRM + pBD );
     
    		return dFO;
    	}
     
    	public double magicCalculationMethod()
    	{
    		String mF1 = magicFormulaSpellDamage.getText();
    		String mF2 = magicFormulaIntellect.getText();
    		String mF3 = magicFormulaPassiveBonus.getText();
    		String mF4 = magicFormulaSpellPower.getText();
    		String mF5 = magicFormulaHealingPower.getText();
     
    		sD =  Integer.parseInt(mF1);
    		iNT =  Integer.parseInt(mF2);
    		pBM = Integer.parseInt(mF3);
    		sP =  Integer.parseInt(mF4);
    		hP =  Integer.parseInt(mF5);
    		int sHP = 1;
     
    		if ( sP == 0 )
    			sHP = hP;
    		else
    			sHP = sP;
     
    		if ( pBM == 0)
    			pBM = 1;
     
    		criticalStrikeTrueOrFalse = criticalStrikeTrue.isSelected();
     
    		if ( criticalStrikeTrue.isSelected() )
    			mFO = ( ( ( ( sD + ( iNT / 2 ) ) * pBM ) * ( sHP / 2 ) ) * 2 );
    		else
    			mFO = ( ( ( sD + ( iNT / 2 ) ) * pBM ) * ( sHP / 2 ) );
     
    		return mFO;
    	}
    }

    @copeg

    There is an exception just as you've said. It reads
    "Exception in thread "main" java.lang.NoClassDefFoundError: DnDFormulator$1 at DnDFormulator.main(DnDFormulator.java:212)"


    I took a minute and looked up possible reasons for this specific type of error and the only reason that I think might be causing it is an error in the manifest file, is there anything wrong with it that you can see?

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program Breaks When In Jar Format

    "Exception in thread "main" java.lang.NoClassDefFoundError: DnDFormulator$1 at
    The java program can not find the class listed in the error message: DnDFormulator$1
    Look in the jar file (use a zip utility program) and see if the missing class file is there.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Program Breaks When In Jar Format

    Within the jar file there is the class DnDFormulator.class and a folder called META-INF which contains the manifest file. There shouldn't be and there never was a class named DnDFormulator$1 so it shouldn't be asking for it.

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program Breaks When In Jar Format

    The compiler generated that class for anonymous inner classes and it must be in the jar file. Look in the folder where the compiler puts the class files and see what's there.
    If you use a wildcard, for example: *.class, when creating the jar file, all class files will be included.

    This will create the class:
    calculateButton.addActionListener(new ActionListener() {
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (December 9th, 2012)

  11. #8
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Program Breaks When In Jar Format

    That's quite the odd error. After looking in my bin folder I found the class that caused the exception, after that I just placed it with in the jar file and the program started working properly. At least I've learned a lesson for next time.

    Thanks a ton to all three of you!

  12. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Program Breaks When In Jar Format

    Quote Originally Posted by tyeeeee1 View Post
    @curmudgeon

    Thanks for the criticism, I've tried to chance the code based on what you've said but I ran into one problem and one question.
    The public variables of the DnDFormulator class, when changed to non-static, throw up a wall of "Non-Static variable variableName cannot be referenced from a static context."; That is why I had them all set to static.
    You're fixing the wrong thing. The solution is not to make everything static, but rather to call methods on object instances. So for instance if you have:

    public class Foo {
      public void myFoo() {
        System.out.println("Foo!");
      }
     
      public static void main(String[] args){ 
        myFoo(); // this will cause your "Non-Static variable variableName cannot be referenced..." error
      }
    }

    Trying to call the non-static myFoo() method in the static main method will cause the "Non-Static variable variableName cannot be referenced..." error to be seen. The solution isn't to make myFoo() static, but to call it only on a Foo object:

    public class Foo {
      public void myFoo() {
        System.out.println("Foo!");
      }
     
      public static void main(String[] args){ 
        Foo foo = new Foo();
        foo.myFoo(); // Now no error occurs
      }
    }

  13. #10
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Program Breaks When In Jar Format

    I just edited almost every line of the program with what I think your example is showing but it's just throwing error after error. Is it fine if I leave those variables static or is there some specific reason why they can't or shouldn't be static?

  14. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Program Breaks When In Jar Format

    Quote Originally Posted by tyeeeee1 View Post
    I just edited almost every line of the program with what I think your example is showing but it's just throwing error after error. Is it fine if I leave those variables static or is there some specific reason why they can't be static?
    Yes, you should not have them as static as it breaks Object-Oriented principles, and will prevent you from subclassing or creating multiple instances of a class.

  15. The Following User Says Thank You to curmudgeon For This Useful Post:

    tyeeeee1 (December 9th, 2012)

  16. #12
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Program Breaks When In Jar Format

    Alright, thanks for the information. I think I'll try re-writing the program and improve upon what I've done so far.

  17. The Following User Says Thank You to tyeeeee1 For This Useful Post:

    curmudgeon (December 9th, 2012)

Similar Threads

  1. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  2. Cookie Jar Program Issues
    By NoranPrease in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 16th, 2012, 02:04 PM
  3. Proper way to get rid of breaks.
    By Massaslayer in forum Loops & Control Statements
    Replies: 6
    Last Post: December 23rd, 2011, 07:53 PM
  4. Program.jar -command
    By luigi10011 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 23rd, 2011, 09:49 AM
  5. How to run program from jar file?
    By jammy in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: March 28th, 2011, 08:39 AM