Why is my Runnable Jar file not working

I followed about 10 different lessons, and none of them seemed to help, my jar executable just didn't work.

My game works fine when I run it in Eclipse and I was able to turn it into a JAR executable that worked just a day or two ago and didn't change too much code. When I try to run .jar, a Java icon appears at the bottom of the screen but then nothing happens.

If my launcher does not open (if I launch without a launcher, it works fine), then there are no errors if I open it using a terminal or an 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 use a Mac if this helps ...

Here are the screenshots I took about the exact steps I took

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

It looks like at the top of my screen along with the Java icon at the bottom, but nothing else happens.

-Thanks

+5
source share
4 answers

I assume that you are creating a GUI application and launching the Jar by clicking on it? Many times, if a GUI application has an error on startup, it obviously does not do anything like this, for example, an error message appears because it was not possible to build any GUI to report an error. I would suggest that something you changed throws an exception at startup, and this message is not visible because you are launching the jar by clicking on it.

If you run the Jar from the terminal (spotlight: "terminal"), you can see a stack trace that will prevent the Jar from starting, and I hope this helps 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 an exception in your main method and display a GUI warning , however this will not work if an exception occurs before the main method is launched (for example, during class loading). In other words, it is nice, but does not completely solve the problem. You can still run 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 } } 
+17
source

To run the .jar file, at a command prompt, type:

 java -jar 'jar_file'.jar 

Where 'jar_file' is the name of your jar file without quotes. In your case, it looks like run.jar .

.jar files are .zip archives that can confuse the operating system

Jar file format

+3
source

Try removing the .class files from the bin folder and restarting the project and cleaning up the project. This is a problem with eclipse. Even when I ran into one problem, I deleted the files and followed the indicated process.

0
source

Check your Java version.

If your program is compiled for Java 8, and the version of Java on your computer is Java 9 or higher, then it may not work.

0
source

Source: https://habr.com/ru/post/1482271/


All Articles