Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By gimbal2

Thread: A Java Exception Has Occured [Error]

  1. #1
    Zacckron is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default A Java Exception Has Occured [Error]

    Hello. I created a new java game, it's known as MiniTennis. It runs completely without any errors when it is run from Eclipse. But when I export and run it, it says "A Java Exception Has Occured.". Please help.

    Here's the code :

    Java Code:
    package com.zacckron.[url]www.game;[/url]
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    @SuppressWarnings ("serial")
    public class Game extends JPanel {
    	
    	Ball ball = new Ball(this);
    	Racquet racquet = new Racquet(this);
        int speed = 1;
        
        private int getScore() {
        	return speed - 1;
        }
    	
    	
    	
    	public Game() {
    		addKeyListener(new KeyListener() {
    			@Override
    			public void keyTyped(KeyEvent e) {
    			}
    
    			@Override
    			public void keyReleased(KeyEvent e) {
    				racquet.keyReleased(e);
    			}
    
    			@Override
    			public void keyPressed(KeyEvent e) {
    				racquet.keyPressed(e);
    			}
    		});
    		setFocusable(true);
    	}
    	
    	private void move() {
    		ball.move();
    		racquet.move();
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		ball.paint(g2d);
    		racquet.paint(g2d);
    		
    		g2d.setColor(Color.GRAY);
    		g2d.setFont(new Font("Verdana", Font.BOLD, 30));
    		g2d.drawString(String.valueOf(getScore()), 10, 30);
    		
    	}
    
    	public void gameOver() {
    		JOptionPane.showMessageDialog(this, "Your Score Is: " + getScore(), "Game Over", JOptionPane.YES_NO_OPTION);
    	System.exit(ABORT);
    	
    	
    	
    	
    }
    	public static void main(String[] args) throws InterruptedException {
    		JFrame frame = new JFrame("Mini Tennis");
    		Game game = new Game();
    		frame.add(game);
    		frame.setSize(300, 400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		while (true) {
    			game.move();
    			game.repaint();
    			Thread.sleep(10);
    		}
    	}
    
    	
    
    int x = 0;
    
    int y = 0;
    
    int xa = 1;
    
    int ya = 1;
    
    
    
    	@SuppressWarnings("unused")
    	private void moveBall() {
    		if (x + xa < 0)
    			xa = 1;
    		if (x + xa > getWidth () - 30)
    			xa = -1;
    		if (y + ya < 0)
    			ya = 1;
    		if (y + ya > getHeight () - 30)
    			ya = -1;
    		
    		
    		
    		
    		
    		x = x + xa;
    		y = y + ya;
    		
    	}
    
    
    
    
    		
    		
    	}




    I have two more classes for the ball and the racquet, but I don't think it's of any use.
    Last edited by JosAH; 07-29-2014 at 09:08 AM. Reason: fixed the [code] ... [/code] tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: A Java Exception Has Occured [Error]

    If there's an exception then there's a stack trace and actual exception message.

    If you can't find them then execute it from the command line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Zacckron is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: A Java Exception Has Occured [Error]

    Quote Originally Posted by Tolls View Post
    If there's an exception then there's a stack trace and actual exception message.

    If you can't find them then execute it from the command line.


    I'm sorry but how do I do that ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: A Java Exception Has Occured [Error]

    It should be the first thing you learn how to do...

    But, open a command window and go to the directory containing your jar file (presumably) and then:
    java -jar <your jar file>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: A Java Exception Has Occured [Error]

    Quote Originally Posted by Tolls View Post
    It should be the first thing you learn how to do...
    ... after learning basic computer science. Otherwise the command prompt itself is already going to be a huge mystery.

    If you want to write your resume in a word processor or check Facebook then knowing how to turn on the computer is enough. If you want to do programming, you need to know a whole lot more. The architecture of a computer and its hardware, how an operating system works, the lot.
    milovan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Zacckron is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: A Java Exception Has Occured [Error]

    Quote Originally Posted by Tolls View Post
    It should be the first thing you learn how to do...

    But, open a command window and go to the directory containing your jar file (presumably) and then:
    java -jar <your jar file>

    Yes. I know it. Just got confused. But when I do that, it says could not run jarfile MyGame.java.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: A Java Exception Has Occured [Error]

    You said you'd "export and run it", which implies to me you exported an executable jar file.
    That's what you need to run, not you source file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Zacckron is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: A Java Exception Has Occured [Error]

    Quote Originally Posted by Tolls View Post
    You said you'd "export and run it", which implies to me you exported an executable jar file.
    That's what you need to run, not you source file.
    That's the thing I did. I exported it. When I double click on it, it shows the java exception.

  9. #9
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: A Java Exception Has Occured [Error]

    Tolls points you, don't run source file
    MyGame.java

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: A Java Exception Has Occured [Error]

    Lets just summarize this thread so far.

    Q: I get an exception when running a Java program outside of Eclipse
    A: try running it in the command prompt so you can see the exception

    Q: I'm sorry but how do I do that ? Translated: So you don't know what is talked about
    A: you do this and that

    Q: Yeah sorry I know (... what?), but it says could not run jarfile MyGame.java. . In other words: you are running a java source file, not a jar. Or the error is mistyped which means you don't re-read what you post
    A: don't run a .java source file, run the exported jar file

    Q: That's the thing I did. I exported it. When I double click on it, it shows the java exception. just no response to what is said at all, not any indication of understanding that running a .java file is not correct
    Tolls likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 07-05-2014, 11:05 PM
  2. A Java Exception Has Occured
    By supervegito in forum New To Java
    Replies: 9
    Last Post: 03-25-2013, 03:11 PM
  3. Java Exception Has Occured Problem Pls Help
    By Kobefernandez in forum New To Java
    Replies: 5
    Last Post: 01-07-2013, 03:01 AM
  4. Java Exception Has Occured
    By EdenC in forum Eclipse
    Replies: 9
    Last Post: 08-30-2011, 04:40 PM
  5. Java Exception Has Occured
    By EdenC in forum New To Java
    Replies: 12
    Last Post: 08-29-2011, 04:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •