I am having a mysterious problem with my JAR file not running. My game opens a JFrame object, in which all of the graphics are displayed. Everything works in Eclipse, but when I try to export my finished game as a Runnable JAR file, and then double click it from my desktop, nothing happens. I don't get an error message or anything, and my JFrame doesn't pop up, so there is no game to play.
I made sure non of my classes had any errors or warnings, and still, the JFrame doesn't open. In addition, when I right-click on the supposedly Runnable JAR file that is on my desktop, and go to properties, and then click the tab that says security, there are all check marks under the allow category under SYSTEM.
Here is my main class, if you need it, though I doubt there is a problem with the actual coding, because it works fine in Eclipse, it just doesn't open from my desktop:
package com.wierd0games.Under.main;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;
import javax.swing.JFrame;
import com.wierd0games.Under.graphics.Draw;
import com.wierd0games.Under.graphics.Sprite;
import com.wierd0games.Under.graphics.SpriteSheet;
import com.wierd0games.Under.input.Keyboard;
import com.wierd0games.Under.mobs.Frog;
import com.wierd0games.Under.mobs.Healthbar;
public class Main extends Canvas implements Runnable {
private static final long serialVersionUID=1L;
public static final int WIDTH=300;
public static final int HEIGHT=WIDTH*9/16;
public static final int SCALE=3;
private JFrame frame;
private boolean running=false;
private Thread thread;
private Draw draw;
private Keyboard keyboard;
private int frames;
private int updates;
private final long TIME_BETWEEN_UPDATES=1000000000/60;
BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
int[] pixels=((DataBufferInt)image.getRaster().getDataBuffer()).getData();
Frog frog;
private int[] sideCollisions=new int[0];
private int[] verticalCollisions=new int[0];
private final int WATER_LEVEL=1600;
private Generator generator;
private Healthbar healthbar;
private int score=0;
private int time=60;
private int lastScore=0;
private int lastTimeAlive=0;
private int room=0;
private int counter=0;
private Sprite openingScreenToDraw=new Sprite(1, 1, 300, SpriteSheet.backgrounds);
Random random=new Random();
public static void main(String[] args) {
Main game=new Main();
game.frame.setResizable(false);
game.frame.setTitle("Under | FPS: --- Updates: --");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
private Main() {
frame=new JFrame();
Dimension size=new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
frame.setPreferredSize(size);
keyboard=new Keyboard();
addKeyListener(keyboard);
draw=new Draw(WIDTH, HEIGHT, pixels);
frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
generator=new Generator(WATER_LEVEL);
healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
}
private void update() {
if (room==0) {
if (++counter>300) {
room=1;
counter=-60;
openingScreenToDraw=Sprite.titleScreen;
}
else {
if (counter>120&&counter<280) {
for (int i=0; i<openingScreenToDraw.pixels.length; i++) {
if (openingScreenToDraw.pixels[i]!=0) {
if (random.nextInt((300-counter-20))==0) {
openingScreenToDraw.pixels[i]=0;
}
}
}
}
}
}
if (room==1) {
keyboard.update();
if (++counter>=0) {
if (counter>40) {
counter=0;
if (openingScreenToDraw==Sprite.titleScreen) {
openingScreenToDraw=Sprite.titleScreen2;
}
else {
openingScreenToDraw=Sprite.titleScreen;
}
}
if (keyboard.keysPressed[10]) {
room=2;
counter=0;
}
}
}
if (room==2) {
if (++counter>=60) {
counter=0;
time--;
}
keyboard.update();
for (int i=0; i<keyboard.keys.length; i++) {
if (keyboard.keysPressed[i]) {
System.out.println("Key "+i+" Pressed");
}
}
int[] returned=generator.update((int)frog.x, (int)frog.y);
int fliesToEat=returned[0];
int chestsToCollect=returned[1];
score+=fliesToEat*10+chestsToCollect*100;
frog.update(sideCollisions, verticalCollisions, fliesToEat);
healthbar.update(frog.hunger, frog.breath, (int)(frog.jumpCharge*100)-40);
draw.update(((int)frog.x+(frog.WIDTH+frog.BLANK_LEFT)/2)*-1+WIDTH/2, ((int)frog.y+(frog.HEIGHT+frog.BLANK_TOP)/2)*-1+HEIGHT/2);
sideCollisions=generator.getSideCollisions();
verticalCollisions=generator.getVerticalCollisions();
if (frog.hunger<=0) {
room=3;
counter=0;
lastScore=score;
lastTimeAlive=60-time;
score=0;
time=60;
frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
generator=new Generator(WATER_LEVEL);
healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
}
if (frog.breath<=0) {
room=4;
counter=0;
lastScore=score;
lastTimeAlive=60-time;
score=0;
time=60;
frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
generator=new Generator(WATER_LEVEL);
healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
}
if (time<=0) {
room=5;
counter=0;
lastScore=score;
lastTimeAlive=60-time;
score=0;
time=60;
frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
generator=new Generator(WATER_LEVEL);
healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
}
}
if (room==3) {
keyboard.update();
if (keyboard.keysPressed[10]) {
room=1;
}
}
if (room==4) {
keyboard.update();
if (keyboard.keysPressed[10]) {
room=1;
}
}
if (room==5) {
keyboard.update();
if (keyboard.keysPressed[10]) {
room=1;
}
}
if (room==6) {
keyboard.update();
if (keyboard.keysPressed[10]) {
room=1;
}
}
}
private void render() {
BufferStrategy bs=getBufferStrategy();
if (bs==null) {
createBufferStrategy(3);
return;
}
for (int i=0; i<pixels.length; i++) {//sets the background color to white so that the clouds don't have holes in them
pixels[i]=-1;
}
if (room==0) {
draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
}
if (room==1) {
draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
}
if (room==2) {
draw.drawSpriteToScreen(0, Math.min(0, -1*(int)frog.y+WATER_LEVEL-HEIGHT/2-2), Sprite.sky);
draw.drawSpriteToScreen(0, Math.max(0, -1*(int)frog.y+HEIGHT+WATER_LEVEL-HEIGHT/2-2), Sprite.water);
frog.drawSelf(draw);
generator.drawBlocks(draw);
healthbar.drawSelf(draw, score, time);
}
if (room==3) {
draw.drawSpriteToScreen(0, 0, Sprite.youStarved);
draw.drawNumberToScreen(80, 64, lastScore);
draw.drawNumberToScreen(117, 79, lastTimeAlive);
}
if (room==4) {
draw.drawSpriteToScreen(0, 0, Sprite.youDrowned);
draw.drawNumberToScreen(80, 64, lastScore);
draw.drawNumberToScreen(117, 79, lastTimeAlive);
}
if (room==5) {
draw.drawSpriteToScreen(0, 0, Sprite.outOfTime);
draw.drawNumberToScreen(80, 64, lastScore);
draw.drawNumberToScreen(117, 79, lastTimeAlive);
}
if (room==6) {
draw.drawSpriteToScreen(0, 0, Sprite.youWereEaten);
draw.drawNumberToScreen(80, 64, lastScore);
draw.drawNumberToScreen(117, 79, lastTimeAlive);
}
Graphics g=bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
private synchronized void start() {
running=true;
thread=new Thread(this, "Under");
thread.start();
}
private synchronized void stop() {
running=false;
try {
thread.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
long lastNanoTime=System.nanoTime();
long lastMilliTime=System.currentTimeMillis();
updates=0;
frames=0;
requestFocus();
while (running) {
long currentNanoTime=System.nanoTime();
while (currentNanoTime-lastNanoTime>=TIME_BETWEEN_UPDATES) {
lastNanoTime+=TIME_BETWEEN_UPDATES;
update();
updates++;
}
render();
frames++;
while (lastMilliTime+1000<System.currentTimeMillis()) {
lastMilliTime+=1000;
frame.setTitle("Under | FPS: "+frames+" Updates: "+updates);
updates=0;
frames=0;
}
}
stop();
}
}
Here is a link to where you can download the JAR: https://sites.google.com/site/wierd0games/ludum-dare-under