1

I'm trying to make a game similar to Wumpus, and so far, it works perfectly in Eclipse. I am currently using JavaFX only for its Media and MediaPlayer class. I decided to export the game as a runnable JAR to see if it would work, but when I double-clicked the JAR to run the program, nothing happened. If I ran the runnable JAR file through command prompt however, the program opened up and worked fine. When I went back in the code, removed anything JavaFX-related, and then exported it as a runnable JAR though, the JAR opened and the program ran fine, without any need for command prompt. If anyone could help me figure out why this happens, and how I can fix this, it would be much appreciated! Here is the code from the main class, if it helps at all(its probably really messy)

package game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class Game extends Application implements ActionListener, KeyListener {

public static Game game;
private JFrame frame;
private Timer timer;
private Renderer renderer;
private Tile[][] tiles;
private Player player;
private Media themeSong;
private MediaPlayer musicPlayer;
public JLabel explosionGIF;

public Game() {
    frame = new JFrame(GameData.FRAME_NAME);
    explosionGIF = new JLabel();
    timer = new Timer(GameData.UPDATE_SPEED_MS, this);
    renderer = new Renderer();
    renderer.setPreferredSize(new Dimension(GameData.FRAME_WIDTH, GameData.FRAME_HEIGHT));
    frame.add(renderer);
    frame.setResizable(true);
    frame.setVisible(true);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.addKeyListener(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    initialize();
    timer.start();
}

private void initialize() {
    tiles = new Tile[GameData.TILE_AMOUNT][GameData.TILE_AMOUNT];
    for (int r = 0; r < tiles.length; r++) {
        for (int c = 0; c < tiles[r].length; c++) {
            tiles[r][c] = new Tile(r, c);
        }
    }
    player = new Player();
    GameData.FRAME_WIDTH_DIFFERENCE = frame.getWidth() - GameData.FRAME_WIDTH;
    GameData.FRAME_HEIGHT_DIFFERENCE = frame.getHeight() - GameData.FRAME_HEIGHT;
}

private void startMusic() {

    musicPlayer.play();
}

public void render(Graphics g) {
    for (Tile[] tileArr : tiles)
        for (Tile tile : tileArr)
            tile.render(g);
    player.render(g);
}

private void update() {
    updateSize();
    if (musicPlayer != null && (musicPlayer.getCurrentTime().greaterThanOrEqualTo(musicPlayer.getStopTime())
            || musicPlayer.getCurrentTime().compareTo(musicPlayer.getStartTime()) == 0)) {
        startMusic();
    }
}

private void updateSize() {
    if (frame.getPreferredSize().getWidth() != GameData.FRAME_WIDTH
            || frame.getPreferredSize().getHeight() != GameData.FRAME_HEIGHT) {
        renderer.setPreferredSize(new Dimension(frame.getWidth() - GameData.FRAME_WIDTH_DIFFERENCE,
                frame.getHeight() - GameData.FRAME_HEIGHT_DIFFERENCE));
        GameData.FRAME_WIDTH = (int) frame.getWidth() - GameData.FRAME_WIDTH_DIFFERENCE;
        GameData.FRAME_HEIGHT = (int) frame.getHeight() - GameData.FRAME_HEIGHT_DIFFERENCE;
        GameData.TILE_WIDTH = GameData.FRAME_WIDTH / GameData.TILE_AMOUNT;
        GameData.TILE_HEIGHT = GameData.FRAME_HEIGHT / GameData.TILE_AMOUNT;
    }
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_LEFT:
        player.move(GameData.MovementDirections.LEFT);
        break;
    case KeyEvent.VK_RIGHT:
        player.move(GameData.MovementDirections.RIGHT);
        break;
    case KeyEvent.VK_UP:
        player.move(GameData.MovementDirections.UP);
        break;
    case KeyEvent.VK_DOWN:
        player.move(GameData.MovementDirections.DOWN);
        break;
    case KeyEvent.VK_SPACE:
        break;
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    renderer.repaint();
    update();
}

public JFrame getFrame() {
    return frame;
}

public Tile[][] getTiles() {
    return tiles;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            game = new Game();
        }
    });
    Application.launch();
}

@Override
public void start(Stage primaryStage) throws Exception {
    // TODO Auto-generated method stub
    themeSong = new Media(getClass().getResource("/sound/theme.mp3").toExternalForm());
    musicPlayer = new MediaPlayer(themeSong);
}

}
  • What is the full command you’re entering in order to run on the command line? – VGR May 24 '19 at 20:42
  • java -jar Wumpus.jar – Vedh Koutha May 24 '19 at 20:48
  • 1
    I suspect your system is configured to invoke Java 11 or later when a .jar file is double-clicked. Java 11 no longer includes JavaFX; it must be downloaded separately (for free). It would appear that your command line, however, invokes a version earlier than 11, probably due to your `PATH` environment variable. – VGR May 24 '19 at 20:54
  • How would I go about fixing this? If I were to run this on another laptop that didn't have JavaFX downloaded, would it no longer work? – Vedh Koutha May 24 '19 at 21:17
  • Correct, it would no longer work. You will need to distribute the JavaFX runtime with your application. JavaFX includes native libraries in addition to .jar files, so you can either create a [native image](https://stackoverflow.com/questions/53453212/how-to-deploy-a-javafx-11-desktop-application-with-a-jre) in Java 11, or you can distribute your application with a copy of the JavaFX runtime, and add a short script for running your application with the proper `-classpath` option and `-Djava.library.path=…` option. – VGR May 25 '19 at 00:02

0 Answers0