JAR problem?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 8497 Views - Last Post: 22 June 2011 - 12:49 AM Rate Topic: -----

#1 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

JAR problem?

Posted 15 June 2011 - 04:58 PM

Hi. I've been able to develop a simple racing game in Java through help from this site and what I could learn from tutorials, but now I've found myself stuck again (the forums with my questions are here http://www.dreaminco...arch_filter_app[forums]=1&view_by_title=1). I have given my project a webstart so that I can send it to friends who would like to see it. But I'm experiencing problems with the performance.

I am able to run my game through the compiler (I use Netbeans) and it seems to work fine, with music and effects (I used http://www.javazoom..../javalayer.html for the MP3s to be played), but when I clean and build to create a JAR and run it without the compiler's help the cars, which move in the compiler, don't move, and no action happens. AT first I thought it was the sound, I was using the audiostream library from sun (see here http://stackoverflow...-sound-in-java) to play wavs for the effects at first, but I had noticed then that when I wanted to make a JAR it would be created, but for some reason the compiler deletes whatever that has to do with it, so I decided to use this code instead (http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml) to play the wavs. It works nicely in the compiler, and doesn't mention any infringement of copyright or anything that would affect the code. So I cleaned and built again to see if the JAR would run well, but I get the same error.

I'm using an ActionListener with a swing Timer to recognize the actions every 5 milliseconds, and I didn't start my project as a Desktop Application (i'm not sure if that would have to do with this). My project also depends heavily on the dispose() and new creations of JFrame classes to work (I used that to transfer from level to level), so would that be the problem? I've also been thinking that maybe since the MP3 playing depends on another JAR (the one in http://www.javazoom....javalayer.html) that maybe the creation of the game's JAR interferes with the usage of this other JAR, and the error of the sounds not being played stop the game from working, would that be possible? Thank you in advance for any help!

I'm posting the class for the Level 1 here, but if you want to see the others, go here: http://www.dreaminco...1&#entry1262481 . I haven't posted them here because nothing really serious has changed in them.

package racemaster;

import java.awt.Color; //For background color
import java.awt.Graphics; //To draw
import java.awt.event.ActionEvent; //To get to do events, such as movement
import java.awt.event.ActionListener; //To be able to recognize events
import java.awt.event.KeyAdapter; //To recognize events through keyboard ket presses
import java.awt.event.KeyEvent; //To be able to determine if a key pressed will make/cause an event
import java.awt.image.BufferedImage; //To add images to the objects
import java.io.*; //To successfully use the FileInputStream
import java.util.ArrayList; //To store the walls, further used for collisions
import javax.imageio.ImageIO; //To read images
import javax.swing.JOptionPane; //To display message boxes
import javax.swing.JPanel; //To add every object used in the level
import javax.swing.Timer; //To determine the interval in which actions occur
import javazoom.jl.decoder.JavaLayerException; //JLayer Exception in case of a mp3 failure
import javazoom.jl.player.Player; //To play mp3s
//import sun.audio.*; //to play wavs (AudioStream)

public class Level1Board extends JPanel implements ActionListener{

    private Timer timer; //Times interval for actions

    private ArrayList walls = new ArrayList(); //Stores walls for use with collision

    int carchosen = 1;
    boolean lapdecrease;
    boolean speedincrease;
    String playercarcolor = "blue";
    String AIcarcolor = "red";
    int angle = 90; //Starting angle of cars

    //Addresses for the images of the finish line, blue car, red car, and wall
    String lineimageaddress = "images/Miscellaneous/finishline.gif";
    BufferedImage line = getImage(lineimageaddress);
    String playercarimagesaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + angle + ".png";
    BufferedImage playercar = getImage(playercarimagesaddress);
    String AIcarimageaddress = "images/Cars/" + AIcarcolor +"car/" + AIcarcolor + "car" + angle + ".png";
    BufferedImage AIcar = getImage(AIcarimageaddress);
    String walladdress = "images/Walls/greenmarble.png";
    BufferedImage wall = getImage(walladdress);

    //Addresses for the images of the lap counters
    //bluecarlapnumer and redcarlapnumber will increase and then be used to change the lap display
    //of both
    int playercarlapnumber = 0;
    String playercarlapsaddress = "images/Laps/" + playercarcolor + "lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";    
    BufferedImage playerlapcounter = getImage(playercarlapsaddress);
    int AIcarlapnumber = 0;
    String AIcarlapsaddress = "images/Laps/" + AIcarcolor + "lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
    BufferedImage AIlapcounter = getImage(AIcarlapsaddress);


    //Useful to play the 'readysetgo.mp3' before the cars move (especially the AI)
    private boolean racestarted = false;
    
    private String raceoutcome = "?";

    //Declarations for the cars, walls, and finish line
    private Car car1 = new Car(playercar, 0, 0, 0, "", false);
    private Car car2 = new Car(AIcar, 0, 0, 0, true);
    private Wall wall1 = new Wall(wall, 0, 0, false);
    private FinishLine finishline = new FinishLine(line, 100, 200, false);

    //Declarations for the lap counters
    private LapCounter playercarlapcounter = new LapCounter(playerlapcounter, 0, 0, false);
    private LapCounter AIcarlapcounter = new LapCounter(AIlapcounter, 0, 0, true);

    //The two checkpoints
    private boolean linecheckpoint = false;
    private boolean midcheckpoint = true;

    FileInputStream fi; //Allows to play wavs as well as mp3s without the Player class of the JAR
    Player readysetgo; //Plays the first sound that indicates the beginning of race
    //AudioStream as; //In charge of any other sound, except the ones not declared here
    AePlayWave wv;
    
    MP3 nightoffire;
    boolean backgroundsongisplaying = false;

    String level1 =      "WWWWWWW\n" //Level 1 structure. Much easier to build
                       + "W     W\n" //than in GameMaker =)
                       + "WCWWW W\n"
                       + "W     W\n"
                       + "WWWWWWW\n";
    int spritesize = 100; //Taken from the walls to increment width and height of the Board
    int w = 0; //Width of the Board
    int h = 0; //Height of the Board

    int finishlinex = 0;
    int finishliney = 0;

    public Level1Board(int carchosen, boolean lapdecrease, boolean speedincrease){

        this.carchosen = carchosen;
        this.lapdecrease = lapdecrease;
        this.speedincrease = speedincrease;
        if(carchosen == 1)playercarcolor = "blue";
        else if(carchosen == 2)playercarcolor = "red";
        else if(carchosen == 3)playercarcolor = "yellow";
        else if(carchosen == 4)playercarcolor = "green";
        else if(carchosen == 5)playercarcolor = "pink";
        else if(carchosen == 6)playercarcolor = "purple";
        else if(carchosen == 7)playercarcolor = "turquoise";
        else if(carchosen == 0)playercarcolor = "gold";
        
        if(lapdecrease) playercarlapnumber++;

        playercarimagesaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + angle + ".png";
        playercar = getImage(playercarimagesaddress);
        if(carchosen != 2)AIcarcolor = "red";
        else AIcarcolor = "blue";
        AIcarimageaddress = "images/Cars/" + AIcarcolor +"car/" + AIcarcolor + "car" + angle + ".png";
        AIcar = getImage(AIcarimageaddress);

        playercarlapsaddress = "images/Laps/" + playercarcolor + "lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";
        playerlapcounter = getImage(playercarlapsaddress);
        playercarlapcounter = new LapCounter(playerlapcounter, 0, 0, false);
        AIcarlapsaddress = "images/Laps/" + AIcarcolor + "lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
        AIlapcounter = getImage(AIcarlapsaddress);
        AIcarlapcounter = new LapCounter(AIlapcounter, 0, 0, true);

        //First sound assgined to the FileInputStream here
         try{
            fi = new FileInputStream("src\\racemaster\\music\\readysetgo.mp3");
            readysetgo = new Player(fi);
        }catch(Exception ex){
                System.out.println("exception");
        }
         
        nightoffire = new MP3("src\\racemaster\\music\\Initial D - Night of Fire.mp3"); 

        addKeyListener(new TAdapter()); //Allows to recognize key presses
        setFocusable(true); //Receives focus from hardware (keyboard) to allow for actions


        timer = new Timer(5, this); //will execute the actionPerformed method every 5 milliseconds
        timer.start(); //starts the timer, duh

        startLevel();
    }

    //Constructs level reading the level1 String with a char
    public final void startLevel(){
        int x = 0;
        int y = 0;

        for(int i = 0; i < level1.length(); i++){
            char item = level1.charAt(i);

            if(item == 'C'){
                y += 25;
                x += 25;
                car2 = new Car(AIcar, x, y, angle, true);
                x += 25;
                car1 = new Car(playercar, x, y, angle, "N", false);
                y -= 25;
                x -= 50;
                x += spritesize;
                finishlinex = car1.x() - 50;
                finishliney = car1.y() - 25;
                if(speedincrease) car1.setSpeed(car1.getSpeed() + 1);
            }else if (item == ' '){
                x += spritesize;
            }else if(item == 'W'){
                wall1 = new Wall(wall, x, y, false);
                walls.add(wall1);
                x += spritesize;
            }else if(item == '\n'){
                y += spritesize;
                if(w < x){
                    w = x;
                }
                x = 0;
            }
            h = y;
        }
    }

    /*Draws every object declared in the build level and those that have changed 
     *position/picture during its execution 
     */
    public final void buildLevel(Graphics g){

        g.setColor(new Color(0, 100, 50)); //I love green
        //Sets the whole panel with the assigned color
        g.fillRect(0, 0, this.getWidth(), this.getHeight());

        ArrayList level = new ArrayList(); //Will hold most objects to be drawn
        level.addAll(walls);
        level.add(finishline);
        level.add(car1);
        level.add(car2);        

        for(int i = 0; i < level.size(); i++){
            Actor item = (Actor) level.get(i);
            if(item instanceof Car){ //Draws the car wherever it is
                if(!item.isAI()){
                    g.drawImage(car1.getBufferedImage(), item.x(), item.y(), this);
                }else{
                    g.drawImage(car2.getBufferedImage(), item.x(), item.y(), this);
                }                
            }            
            if(item instanceof Wall){ //Draws the Walls in their place
                g.drawImage(wall1.getBufferedImage(), item.x(), item.y(), this);
            }
            if(item instanceof FinishLine){ //Draws the finish line
                g.drawImage(finishline.getBufferedImage(), finishlinex, finishliney, this);
            }
        }
        
        //The following 2 lines will draw the lap counters
        g.drawImage(playercarlapcounter.getBufferedImage(), 0, h, this);
        g.drawImage(AIcarlapcounter.getBufferedImage(), w - AIcarlapcounter.getBufferedImage().getWidth(), h, this);
        
    }

    //Takes a String as directory and gets an image from it
    private BufferedImage getImage(String filename) {
        try{
            InputStream in = getClass().getResourceAsStream(filename);
            return ImageIO.read(in);
        }catch(IOException io){
            System.out.println("The Image was not loaded.");
            System.exit(1);
        }
        return null;
    }

    //Rotates Car for both the player and the AI
    public void rotateCar(Actor c, int a){
        String newplayercarimageaddress = "";
        String newAIcarimageaddress = "";
        BufferedImage bi = c.getBufferedImage();
        if(!c.isAI()){
            newplayercarimageaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + car1.getAngle() + ".png";
            bi = getImage(newplayercarimageaddress);
            
            //I used 36 because without it the cars would rotate with respect to
            //their top left corner. This number came out by initially subtracting
            //the height or widths of the previous and new images
            if(car1.getPreviousAngle() == 45 && car1.getAngle() == 0){
                car1.y += 36;
            }else if(car1.getPreviousAngle() == 270 && car1.getAngle() == 225){
                car1.x -= 36;
            }else if(car1.getPreviousAngle() == 180 && car1.getAngle() == 135){
                car1.y -= 36;
            }else if(car1.getPreviousAngle() == 135 && car1.getAngle() == 90){
                car1.x += 36;
            }else if(car1.getPreviousAngle() == 90 && car1.getAngle() == 135){
                car1.x -= 36;
            }else if(car1.getPreviousAngle() == 135 && car1.getAngle() == 180){
                car1.y += 36;
            }else if(car1.getPreviousAngle() == 225 && car1.getAngle() == 270){
                car1.x += 36;
            }else if(car1.getPreviousAngle() == 0 && car1.getAngle() == 45){
                car1.y -= 36;
            }
            //It is necessary to call the car with its new image to rotate, but even
            //more necessary to call it with its direction, so that it does not stop
            car1 = new Car(bi, car1.x(), car1.y(), car1.getAngle(), car1.getDirection(), false);
            if(speedincrease) car1.setSpeed(car1.getSpeed() + 1);
            repaint();
            car1.planMovement();
        }else if(c.isAI()){
            //Since this is for the AI, a is only used here. Because the angle manipulation
            //occurs in this class
            newAIcarimageaddress = "images/Cars/" + AIcarcolor + "car/" + AIcarcolor + "car" + a + ".png";
            bi = getImage(newAIcarimageaddress);

            if(car2.getAngle() == 45 && a == 0){
                car2.y += 36;
            }else if(car2.getAngle() == 270 && a == 225){
                car2.x -= 36;
            }else if(car2.getAngle() == 180 && a == 135){
                car2.y -= 36;
            }else if(car2.getAngle() == 135 && a == 90){
                car2.x += 36;
            }else if(car2.getAngle() == 90 && a == 135){
                car2.x -= 36;
            }else if(car2.getAngle() == 135 && a == 180){
                car2.y += 36;
            }else if(car2.getAngle() == 225 && a == 270){
                car2.x += 36;
            }else if(car2.getAngle() == 0 && a == 45){
                car2.y -= 36;
            }
            car2 = new Car(bi, car2.x(), car2.y(), a, true);
            repaint();
        }
    }

    //Increases the laps and changes its picture display accordingly
    public void increaseLap(Actor lc){
        if(!lc.isAI()){
            String newplayercarlapaddress = "images/Laps/" + playercarcolor +"lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";
            BufferedImage bi = getImage(newplayercarlapaddress);
            playercarlapcounter = new LapCounter(bi, playercarlapcounter.x(), playercarlapcounter.y(), playercarlapcounter.isAI());
        }else{
            String newAIcarlapaddress = "images/Laps/" + AIcarcolor +"lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
            BufferedImage bi = getImage(newAIcarlapaddress);
            AIcarlapcounter = new LapCounter(bi, AIcarlapcounter.x(), AIcarlapcounter.y(), AIcarlapcounter.isAI());
        }
        repaint();
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        buildLevel(g);
    }

    //Used when the Board is built in Level1 Class
    public int getBoardWidth() {
        return this.w;
    }

    //Used when the Board is built in Level1 Class
    public int getBoardHeight(){
        return this.h;
    }

    public String RaceOutcome(){
        return raceoutcome;
    }

    public void actionPerformed(ActionEvent e) {
        if(!backgroundsongisplaying){
            nightoffire.play();
            backgroundsongisplaying = true;
        }      

        if(racestarted){
            try {
                readysetgo.play(); //serves as countdown before the race starts
            } catch (JavaLayerException ex) {
                System.out.println("JLayer could not play the sound 'readysetgo.mp3'");
            }
            if(CheckWallCollision(car2)){
                int car2angle = car2.getAngle();
                car2angle -= 45; //basically a turn to the right
                if(car2angle < 0){
                    car2angle = 315;
                }
                rotateCar(car2, car2angle);
            }
            car2.AImove();
            //Checks if car2 passed the finish line
            if(car2.getSpeed() == 1){
                if(car2.y() == finishliney && (car2.x() > finishlinex && car2.x() < finishlinex + 100)){
                    AIcarlapnumber++;
                    if(AIcarlapnumber < 6)increaseLap(AIcarlapcounter);
                }
            }else if(car2.getSpeed() == 2){
                if(car2.y() == finishliney - 1 && (car2.x() > finishlinex && car2.x() < finishlinex + 100)){
                    AIcarlapnumber++;
                    if(AIcarlapnumber < 6)increaseLap(AIcarlapcounter);
                }
            }
            
            //Done when the AI car finishes first
            if(AIcarlapnumber == 6){
                try{
                    //fi = new FileInputStream("src\\racemaster\\music\\doofenshmirtz fail theme.wav");                    
                    //as = new AudioStream(fi);
                    //AudioPlayer.player.start(as);
                    String failtheme = "src\\racemaster\\music\\doofenshmirtz fail theme.wav";
                    wv = new AePlayWave(failtheme);
                    wv.start();
                }catch(Exception ex){
                }
                nightoffire.close();
                raceoutcome = "lost";
                JOptionPane.showMessageDialog(null, "YOU LOST!");                
                timer.stop();                
            }

            if(!CheckWallCollision(car1)){
                //Checks if midpoint check has been passed                
                if((car1.y() >= 200 && car1.y() <= 203) && (car1.x() > w - 200 && car1.x() < w - 100)) midcheckpoint = true;

                //Checks if finish line is passed
                if((car1.y() >= finishliney && car1.y() <= finishliney + 3) && (car1.x() > finishlinex && car1.x() < finishlinex + 100)){
                    linecheckpoint = true;
                    if(linecheckpoint && midcheckpoint){
                        playercarlapnumber++;
                        linecheckpoint = false;
                        midcheckpoint = false;
                        if(playercarlapnumber < 6)increaseLap(playercarlapcounter);
                    }
                }
                
                //When race is completed by player
                if(playercarlapnumber == 6){
                    try{
                        //fi = new FileInputStream("src\\racemaster\\music\\Ta Da.wav");                        
                        //as = new AudioStream(fi);
                        //AudioPlayer.player.start(as);
                        String wintheme = "src\\racemaster\\music\\Ta Da.wav";
                        wv = new AePlayWave(wintheme);
                        wv.start();
                    }catch(Exception ex){
                    }
                    nightoffire.close();
                    raceoutcome = "won";
                    JOptionPane.showMessageDialog(null, "YOU WON!");                    
                    timer.stop();                    
                }
                car1.move();
            }else{
                try{
                    //fi needed to be initialized here so that the bump.wav could
                    //be played whenever car collides
                    //fi = new FileInputStream("src\\racemaster\\music\\bump.wav");
                    //as = new AudioStream(fi);
                    //AudioPlayer.player.start(as);
                    String bump = "src\\racemaster\\music\\bump.wav";
                    wv = new AePlayWave(bump);
                    wv.start();
                }catch(Exception ex){
                }
                car1.bounce();
            }
            if(car1.isTurning()){
                rotateCar(car1, 0);
            }
        }else{
            racestarted = true;
        }
        repaint();
    }

    //Controls collisions of Cars and Walls only
    public boolean CheckWallCollision(Actor c){
        if(!c.isAI()){
            for(int i = 0; i < walls.size(); i++){
                Wall ww = (Wall) walls.get(i);
                if(c.getAngle() == 90){
                    if((c.x() > ww.x() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() && (c.y() < ww.y() + wall1.getBufferedImage().getHeight()))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 45){
                    if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 25 && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 0){
                    if((c.x() == ww.x() - c.getBufferedImage().getWidth() + 10 || c.x() == ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + wall1.getBufferedImage().getHeight()))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 315){
                    if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 10 && c.x() < ww.x() + wall1.getBufferedImage().getWidth() - 20) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 270){
                    if((c.x() > ww.x() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight()) && (c.y() < ww.y() + wall1.getBufferedImage().getHeight())){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 225){
                    if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 10 && c.x() < ww.x() + wall1.getBufferedImage().getWidth() - 20) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 180){
                    if((c.x() > ww.x() - c.getBufferedImage().getWidth() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y()) && (c.y() < ww.y() + wall1.getBufferedImage().getHeight())){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }else if(c.getAngle() == 135){
                    if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 25 && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
                        if(c.getDirection().equals("D")){
                            car1.collisiondirection = "D";
                        }else{
                            car1.collisiondirection = "R";
                        }
                        return true;
                    }
                }
            }
        }else if(c.isAI()){
            for(int i = 0; i < walls.size(); i++){
                Wall ww = (Wall) walls.get(i);
                if(c.getAngle() == 90){
                    if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() + 15))){
                        return true;
                    }
                }else if(c.getAngle() == 45){
                    if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() - 10))){
                        return true;
                    }
                }else if(c.getAngle() == 0){
                    if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 40 && c.x() < ww.x()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
                        return true;
                    }
                }else if(c.getAngle() == 315){
                    if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 20 && c.x() < ww.x()) && (c.y() > ww.y() + 35 && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
                        return true;
                    }
                }else if(c.getAngle() == 270){
                    if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() - ww.getBufferedImage().getHeight() + 30 && (c.y() < ww.y()))){
                        return true;
                    }
                }else if(c.getAngle() == 225){
                    if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 30 && c.x() < ww.x()) && (c.y() > ww.y() - 80 && (c.y() < ww.y()))){
                        return true;
                    }
                }else if(c.getAngle() == 180){
                    if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
                        return true;
                    }
                }else if(c.getAngle() == 135){
                    if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth() - 10) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() + 20))){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    //Class that is in charge of controlling the key presses
    private class TAdapter extends KeyAdapter{

        @Override
        public void keyPressed(KeyEvent e) {
            car1.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            car1.keyReleased(e);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: JAR problem?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: JAR problem?

Posted 15 June 2011 - 05:08 PM

Did you build your jar through your ide, and did you add the external jars to your build path?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 15 June 2011 - 05:16 PM

What does your manifest.mf file looks like ?

Better to do it by hand and never trust the IDE (or the way I use it :))

http://download.orac...asicsindex.html
Was This Post Helpful? 0
  • +
  • -

#4 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JAR problem?

Posted 15 June 2011 - 08:17 PM

@nick2price: I built my JAR through the clean and build option of NetBeans, but I didn't change anything in the build path or any options given. But do you really have to specify if you need to include another JAR in the build even if you're already using it as a source?

@pbl: My manifest says:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
It's just 1KB. Should it be bigger, or should it include anything else? I'm definitely gonna look up your link to try and do it by hand
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 15 June 2011 - 08:27 PM

View Postgfcf14, on 15 June 2011 - 11:17 PM, said:

@pbl: My manifest says:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Obviously the built didn't update it should be
Main-Class: racemaster/theClassHoldingTheMainMethod.class

Sorry my bad

Main-Class: racemaster/theClassHoldingTheMainMethod

*Edited sorry you shouldn't put the .class

This post has been edited by pbl: 18 June 2011 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,819
  • Joined: 20-September 08

Re: JAR problem?

Posted 16 June 2011 - 02:25 AM

Quote

...the error of the sounds not being played stop the game from working, would that be possible?
Yes. Make sure you have included all necessary jars in the jnlp, the link to which doesn't currently work btw...
Was This Post Helpful? 0
  • +
  • -

#7 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JAR problem?

Posted 18 June 2011 - 07:05 PM

Mr. pbl, I created a JAR in the way that was explained in the link you provided me using command prompt, and checked with the jar tf try9.jar (the jar try9 was my 9th unsuccessful try), and it contains all of the files I meant to put in the jar for my project. However as I try to open it I get the following message:

Exception in thread "main" java.lang.NoClassDefFoundError: racemaster/MainMenu/class
Caused by: java.lang.ClassNotFoundException: racemaster.MainMenu.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: racemaster/MainMenu.class. Program will exit.



So I tried opening with JRE. And I get a message box which tells me "Could not find the main class: racemaster/MainMenu.class. Program will exit."

I thought that maybe the Main class wasn't specified. So I created a MF file following your comment: Main-Class: racemaster/MainMenu.class (MainMenu is the main class). So I try creating the JAR again and I get the same thing. I'm not sure why since I'm apparently specifying which is the main class.


@g00se: I'm not really sure how to specify what files to use to build the jar in NetBeans, but in Properties->Build->Packaging I noticed the line that says "Exclude from JAR File:" with the following extensions: **/*.java,**/*.form . I thought that maybe not having the .java files would hinder the performance of my jar, so I erased the **/*.java and built again. I then tried to run the JAR through command prompt, and it runs, but when I get to the part where the first MP3 sound is played (the first sound ever), I get an error message in the command prompt:

exception
Problem playing file src\racemaster\music\Initial D - Night of Fire.mp3
java.io.FileNotFoundException: src\racemaster\music\Initial D - Night of Fire.mp3 (The system cannot find the path specified)
java.lang.NullPointerException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at racemaster.Level1Board.actionPerformed(Level1Board.java:335)
        at javax.swing.Timer.fireActionPerformed(Timer.java:291)
        at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
        at java.awt.EventQueue.access$000(EventQueue.java:84)
        at java.awt.EventQueue$1.run(EventQueue.java:602)
        at java.awt.EventQueue$1.run(EventQueue.java:600)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



The line 335 at Level1Board.java says the following:

readysetgo.play();


readysetgo is a MP3 variable, the first sound of any level. In my MP3 class, the play() method is

public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
        new Thread() {
            public void run() {
                try { player.play(); }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();




    }



It's really weird because when I run the program in NetBeans the sounds play perfectly, showing no error in the directory from where the sound is coming.

Also, I tried running the jar before actually changing the **/*.java part, and I get the same error as above.

Something I forgot to mention. While my jar is building in Netbeans, the compiler gives me the message "Note: Some input files use unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details." But I'm not sure if that should be a problem, should it?

This post has been edited by gfcf14: 18 June 2011 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 18 June 2011 - 08:51 PM

Sorry, my mistake, you do not put .class

so should be

Main-Class: racemaster/theClassHoldingTheMainMethod

adding the .class will look for the class class under theClassHoldingTheMainMethod
Was This Post Helpful? 0
  • +
  • -

#9 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JAR problem?

Posted 18 June 2011 - 09:47 PM

I should've noticed, it becomes redundant to look for a class with the name class. But I'm still getting the same mistake

Exception in thread "main" java.lang.NoClassDefFoundError: racemaster/MainMenu
Caused by: java.lang.ClassNotFoundException: racemaster.MainMenu
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: racemaster/MainMenu. Program will exit.



When I open the file on Netbeans, I can set the main class through the project's properties, but is there any other way to do it? Command prompt doesn't seem to recognize the manifest
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 18 June 2011 - 09:52 PM

Are you sure your

public static void main(String[] args)

method is in MainMenu ?
Was This Post Helpful? 0
  • +
  • -

#11 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JAR problem?

Posted 18 June 2011 - 09:57 PM

Yes. Although the MP3 class contains a psvm method that the programmers of it (http://introcs.cs.princeton.edu/java/faq/mp3/MP3.java.html) wrote for testing purposes. Would this affect the recognition of the Main class? And I also have one in the LastFrame class (the "credits" screen after the game is completed), though this one is commented out.

This post has been edited by gfcf14: 18 June 2011 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 18 June 2011 - 10:11 PM

No thats OK. Actually all Java class should have a main() method for unit testing.
That is why you have to put in the manifest which one to use :)

Can you enter the command you enter at the console prompt to jar your application and from which directory you enter it ?
Was This Post Helpful? 0
  • +
  • -

#13 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: JAR problem?

Posted 18 June 2011 - 10:27 PM

Ok. The project is on my flash drive, which is H:/ . In command prompt, I enter
h:


to access the flash drive. From the I enter
jar cvfm try10.jar H:\COP2805\RaceMaster\src\racemaster\racemastermanifest.mf H:\COP2805\RaceMaster\src\racemaster


where H:\COP2805\RaceMaster\src\racemaster is the address in the flash drive where the rpoject sources are. The manifest racemastermanifest.mf is located in that folder as well.
After the jar building process ends, since I was doing all the commands in h:, the jar is created there. So when I run the jar in command prompt I enter
java -jar H:\try10.jar


When I ran the jars created by Netbeans (named RaceMaster instead), they would run. But I'd still get a FilenotFoundException. When I try to run these, there is a ClassnotFoundException, namely the main one.

This post has been edited by gfcf14: 18 June 2011 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,819
  • Joined: 20-September 08

Re: JAR problem?

Posted 19 June 2011 - 10:22 AM

You should probably create a 'music' directory under src and put your MP3 files in there. Load with

InputStream in = getClass().getResourceAsStream("/music/x.mp3");

This post has been edited by g00se: 19 June 2011 - 10:42 AM
Reason for edit:: Wrong advice

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JAR problem?

Posted 19 June 2011 - 10:34 AM

View Postgfcf14, on 19 June 2011 - 01:27 AM, said:

jar cvfm try10.jar H:\COP2805\RaceMaster\src\racemaster\racemastermanifest.mf H:\COP2805\RaceMaster\src\racemaster


Not good... you do not want to Jar \COP2805\RaceMaster\src\racemast\....
you want racemaster only that is your poackage root

you have to cd to
H:\COP2805\RaceMaster\src
and
jar cvfm try10.jar racemaster/racemastermanifest.mf racemaster

also I have a concern about your /src directory
Are you using an IDE that put the .java and .class in two different directories ?
If it is the case, it is not the .java files that you want but the .class files

This post has been edited by pbl: 19 June 2011 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2