6

I followed about 10 different tutorials and none of them seemed to do the trick, my runnable jar file just isn't working.

My game runs fine when I run it in eclipse, and I was able to make it into a runnable jar file that worked only a day or two ago and have not changed too much code. When I try to run the .jar a java icon pops up at the bottom of my screen but then nothing happens.

It's my launcher is failing to open (if I run without launcher it works fine), there are no errors wether I open with terminal or eclipse.

package com.l3g3nds.threed;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Launcher extends JFrame implements MouseListener, MouseMotionListener {
    private static final long serialVersionUID = 1L;

    protected JPanel window = new JPanel();
    // private JButton play, options, help, quit, credits;
    // private Rectangle rplay, roptions, rhelp, rquit, rcredits;

    protected int buttonWidth = 120;
    protected int buttonHeight = 40;

    public Launcher(int id) {
        JPanel panel = (JPanel) getContentPane();

        JLabel label = new JLabel();
        label.setIcon(new ImageIcon("res/Launcher/schoolbackground.png"));
        System.out.println(Display.lScreen());
        if (Display.lScreen() == 5)
            label.setIcon(new ImageIcon("res/Launcher/quit1.png"));
        if (Display.lScreen() == 4)
            label.setIcon(new ImageIcon("res/Launcher/credits1.png"));
        if (Display.lScreen() == 3)
            label.setIcon(new ImageIcon("res/Launcher/options1.png"));
        if (Display.lScreen() == 2)
            label.setIcon(new ImageIcon("res/Launcher/help1.png"));
        if (Display.lScreen() == 1)
            label.setIcon(new ImageIcon("res/Launcher/beginrunning1.png"));
        panel.add(label);

        setUndecorated(true);

        System.out.println("launched");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);

        addMouseListener(this);
        addMouseMotionListener(this);

    }

    public void drawButtons() {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int cx = e.getX();
        int cy = e.getY();
        if (cx >= 11 && cy >= 14 && cx <= 214 && cy <= 44) {
            dispose();
            new RunGame();
        } else if (cx >= 11 && cy >= 66 && cx <= 70 && cy <= 96) {
            //open help
        } else if (cx >= 11 && cy >= 119 && cx <= 119 && cy <= 144) {
            this.dispose();
            new Options();
        } else if (cx >= 11 && cy >= 170 && cx <= 114 && cy <= 200) {
            //open credits
        } else if (cx >= 11 && cy >= 222 && cx <= 74 && cy <= 254) {
            System.exit(0);
        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        int mx = e.getX();
        int my = e.getY();
        if (mx >= 11 && my >= 14 && mx <= 214 && my <= 44) {
            if (Display.lScreen() != 1) {
                Display.setlScreen(1);
                new Launcher(1);
                System.out.println("1");
            }
        } else if (mx >= 11 && my >= 66 && mx <= 70 && my <= 96) {
            if (Display.lScreen() != 2) {
                Display.setlScreen(2);
                new Launcher(1);
                System.out.println("2");
            }
        } else if (mx >= 11 && my >= 119 && mx <= 119 && my <= 144) {
            if (Display.lScreen() != 3) {
                Display.setlScreen(3);
                new Launcher(1);
                System.out.println("3");
            }
        } else if (mx >= 11 && my >= 170 && mx <= 114 && my <= 200) {
            if (Display.lScreen() != 4) {
                Display.setlScreen(4);
                new Launcher(1);
                System.out.println("4");
            }
        } else if (mx >= 11 && my >= 222 && mx <= 74 && my <= 254) {
            if (Display.lScreen() != 5) {
                Display.setlScreen(5);
                new Launcher(1);
                System.out.println("5");
            }
        }
        else {
            if (Display.lScreen() != 0) {
                Display.setlScreen(0);
                new Launcher(1);
                System.out.println("0");
            }
        }
    }
}

I'm using a mac if that helps...

Here are screen shots that I took of the exact steps I took

Some warnings I got (I think they are only because of unused variables, I got this message earlier when I was able to get my .jar to work)

This appears that the top of my screen along with the java icon at the bottom, but nothing else happens.

-Thanks

java
  • 1,319
  • 6
  • 15
  • 30
  • 5
    But you've left out the most important bit of information. You state that it worked a day or two ago, `"and have not changed too much code."`, but what you did change most be actually quite important. So... what exactly did you change? Without more of this key information, I think we're doomed to be unable to help you. Note, you can delete the images you've posted as they don't contribute at all to finding the source of your problem. – Hovercraft Full Of Eels May 22 '13 at 21:54
  • Did you recently add any jars or classes to your app? – jahroy May 22 '13 at 21:55
  • 7
    run the jar file from the command line (using java -jar theJar.jar), and see if you an error. – JB Nizet May 22 '13 at 21:58
  • I get this error "Unable to access jarfile {Run.jar}" – java May 22 '13 at 22:11
  • 2
    Edit your question and post the cull, complete and exact error with its stack trace. – JB Nizet May 22 '13 at 22:14
  • @JBNizet I added the code of the class that is failing to run – java May 22 '13 at 22:55

3 Answers3

19

I assume you're making a GUI application, and launching the Jar by clicking on it? Many times if a GUI application has an error at startup, it doesn't visibly do anything, like pop an error message, because it hasn't had a chance to build any sort of GUI to report the error to yet. I would guess that something you changed causes an exception at startup, and that message is not visible because you're launching the jar by clicking on it.

If you run the Jar from the terminal instead (spotlight: "terminal") you will be able to see the stack trace that's preventing your Jar from running, and hopefully that will help you debug further.

$ cd directory/of/jar
$ java -jar name_of_jar.jar
  .... stack trace should appear here, or program should run

You can also try to catch the exception in your main method and display a GUI alert, however this doesn't work if the exception is raised before the main method starts (e.g. during class loading). In other words it's a nice-to-have, but doesn't totally solve the problem. You may still need to run the Jar from the command line.

public static void main(String[] args) {
  try {
    // body of main method goes here, including any other error handling
  } catch (Throwable t) {
    JOptionPane.showMessageDialog(
        null, t.getClass().getSimpleName() + ": " + t.getMessage());
    throw t; // don't suppress Throwable
  }
}
Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Okay so it's launching (printing out that it has launched) but the actual launcher for the game is not coming up – java May 22 '13 at 22:49
  • Ahh I found where my problem is but don't know how to fix it. – java May 22 '13 at 22:52
  • @java - Post the stack trace by editing your question. If you post the entire stack trace, it will be easy to identify your problem. – jahroy May 22 '13 at 22:58
  • @jahroy what is a stack trace? – java May 22 '13 at 22:59
  • @java - A stack trace is a large piece of text that gets output when an unhandled Exception is thrown in a Java program. You should be seeing one when you try to launch your jar from the command line. Stack traces are typically very large (they usually contain at least a couple dozen lines of text). They contain a wealth of information about what went wrong, where, and why. For example, a stack trace usually states the exact line of code from which the Exception was thrown. Please paste your entire stack trace into your original question. – jahroy May 22 '13 at 23:04
  • 1
    @java - "_What is a stack trace?_" --> [More info here](http://stackoverflow.com/a/3988794/778118). Learning to read and interpret a stack trace is one of the most fundamental things **you must learn** if you ever want to be a Java developer. – jahroy May 22 '13 at 23:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30447/discussion-between-java-and-jahroy) – java May 22 '13 at 23:11
3

To run a .jar file, in command line write:

java -jar 'jar_file'.jar

Where 'jar_file' is the name of your jar file without any quotes. In your case it appears to be run.jar.

.jar files are .zip archives, which can confuse the operating system

Jar file format

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
Isaac
  • 11,409
  • 5
  • 33
  • 45
0

Check your Java version.

If your program is compiled against Java 8 and the version of Java on your PC is Java 9 or higher then it may be not working.

Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42
John
  • 1