Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Export to jar..

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Export to jar..

    Hi guys. So me again need help So i create a snake game. Ant this game have more than 1 class. So i need this thing export to jar.. So i have question:
    1.Do i have insert to public static void main(String[]args) method to somewhere class?
    2.How to export to jar with more than 1 class..

    Thanks for help
    And again, if I must insert main method in somewhere class, so which class?


    snakeCanvas.java :

    import java.awt.Event;
    import java.awt.Point;
     
    import javax.swing.event.*;
     
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.Random;
     
    import javax.swing.*;
    public class snakeCanvas extends Canvas implements Runnable,KeyListener {
     
    private LinkedList<Point> snake;
     
    private final int BOX_HEIGHT = 15;
    private final int BOX_WIDTH = 15;
    private final int GRID_WIDTH = 25;
    private final int GRID_HEIGHT = 25;
    private int direction = Direction.NO_DIRECTION;
    private Point fruit;
    private Thread runThread;
    private Graphics globalGraphics;
    private int score = 0;
    private String highScore = "";
     
    public void paint(Graphics g) {
    	this.setPreferredSize(new Dimension(640,480));
    	snake = new LinkedList<Point>();
    	GenerateDefaultSnake();
     
     
    	PlaceFruit();
    	this.addKeyListener(this);
     
    	globalGraphics = g.create();
    	if (runThread == null ){
    		runThread = new Thread(this);
    		runThread.start();
    	}
     
    	if (highScore.equals("")) {
    		highScore = this.getHighScore();
    	}
    }
     
    public String getHighScore() {
    	FileReader readFile = null;
    	BufferedReader reader = null;
    	try {
    		readFile = new FileReader("highscore.dat");
    		reader = new BufferedReader(readFile);
    		return reader.readLine();
    	}catch(Exception e) {
    		return "Nobody :0";
    	} finally {
    		try {
    			if (reader != null)
    			reader.close();
    		} catch (IOException e) {
     
    			e.printStackTrace();
    		}
    	}
     
     
     
    }
    public void GenerateDefaultSnake() {
    	snake.clear();
     
    	score = 0;
    	snake.add(new Point(0,2));
    	snake.add(new Point(0,1));
    	snake.add(new Point(0,0));
    	snake.add(new Point(0,0));
    	direction = Direction.NO_DIRECTION;
    }
     
    public void Draw(Graphics g) {
    	g.clearRect(0, 0, BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT +20);
    	//Create new image
     
     
    	//BufferedImage buffer = new BufferedImage(BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT, BufferedImage.TYPE_INT_ARGB);		
    	//Graphics bufferGraphics = buffer.getGraphics();
     
     
    	DrawFruit(g);
    	DrawGid(g);
    	DrawSnake(g);
    	DrawScore(g);
     
     
     
     
     
     
    	//flip
     
    	//g.drawImage(buffer, 0, 0, BOX_WIDTH * GRID_WIDTH, BOX_HEIGHT * GRID_HEIGHT, this);
     
    }
     
    public void move() {
    	Point head = snake.peekFirst();
    	Point newPoint = head;
    	switch(direction) {
     
    	case Direction.NORTH:
    	newPoint = new Point(head.x, head.y - 1);
    	break;
     
    	case Direction.SOUTH:
    		newPoint = new Point(head.x, head.y +1); 
    		break;
     
    	case Direction.WEST:
    		newPoint = new Point(head.x - 1,head.y );
    		break;
     
    	case Direction.EAST:
    		newPoint = new Point(head.x + 1, head.y);
    	}
     
    	snake.remove(snake.peekLast());
     
    	if (newPoint.equals(fruit)) {
    		Point addPoint = (Point) newPoint.clone();
    		score+=10;
     
    		switch(direction) {
     
    		case Direction.NORTH:
    		newPoint = new Point(head.x, head.y - 1);
    		break;
     
    		case Direction.SOUTH:
    			newPoint = new Point(head.x, head.y +1); 
    			break;
     
    		case Direction.WEST:
    			newPoint = new Point(head.x - 1,head.y );
    			break;
     
    		case Direction.EAST:
    			newPoint = new Point(head.x + 1, head.y);
    		}
     
    		snake.push(addPoint);
    		PlaceFruit();
    	} else if (newPoint.x < 0 || newPoint.x > (GRID_WIDTH - 1) ) {
    		CheckScore();
    		GenerateDefaultSnake();
    		return;
     
    	} else if (newPoint.y < 0 || newPoint.y > (GRID_HEIGHT - 1) ) {
    		//oopsssS
    		CheckScore();
    		GenerateDefaultSnake();
    		return;
    	} else if (snake.contains(newPoint)) {
    		CheckScore();
    		GenerateDefaultSnake();
    		return;
    	}
     
     
     
    	//if we reack code
     
    	snake.push(newPoint);
     
    }
     
     
     
    public void DrawScore(Graphics g) {
    	g.drawString("Surinkai "+score+" tasku :) ",0, BOX_HEIGHT * GRID_HEIGHT+10);
    	g.drawString("HighScore: "+highScore, 0 , BOX_HEIGHT * GRID_HEIGHT+20);
    }
     
    public void CheckScore() {
    	if (highScore.equals(""))
    	return;
     
    	if (score > Integer.parseInt(highScore.split(":")[1])) {
    		String name = JOptionPane.showInputDialog("You set new highscore! YOUR NAME");
    		highScore = name +":"+score;
     
    		File scoreFile = new File("highscore.dat");
    		if(!scoreFile.exists()) {
    			try {
    				scoreFile.createNewFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		FileWriter writeFile = null;
    		BufferedWriter writer = null;
    		try {
    			writeFile = new FileWriter(scoreFile);
    			writer = new BufferedWriter(writeFile);
    			writer.write(this.highScore);
     
    		}catch(Exception e) {
     
    		} 
    		finally {
     
    			try 
    			{
    				if (writer != null) {
    					writer.close();	
    			}
     
    		} catch(Exception e) {
     
     
    		}
    		}
    	}
    }
    public void DrawGid(Graphics g) {
    	//Vertical lines
    	g.drawRect(0, 0, GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT);
    	for(int x = BOX_WIDTH; x<GRID_WIDTH * BOX_WIDTH ;x+=BOX_WIDTH) {
    		g.drawLine(x, 0, x, BOX_HEIGHT*GRID_HEIGHT);
     
    	}
     
     
     
    	for (int y = BOX_HEIGHT; y < GRID_HEIGHT * BOX_HEIGHT; y+= BOX_HEIGHT) {
    		g.drawLine(0,y,GRID_WIDTH*BOX_HEIGHT,y);
     
    	}
     
    }
     
    public void DrawSnake(Graphics g) {
    	g.setColor(Color.GREEN);
    	for (Point p : snake) {
    		g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
    	}
    	g.setColor(Color.BLACK);
     
    }
     
     
     
    public void DrawFruit(Graphics g) {
    	g.setColor(Color.RED);
    	g.fillOval(fruit.x * BOX_WIDTH, fruit.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
    	g.setColor(Color.BLACK);
    }
     
    public void PlaceFruit() {
    	Random rand = new Random();
    	int randomX = rand.nextInt(GRID_WIDTH);
    	int randomY = rand.nextInt(GRID_HEIGHT);
    	Point randomPoint = new Point(randomX,randomY);
    	while(snake.contains(randomPoint)) {
    		randomX = rand.nextInt(GRID_WIDTH);
    		randomY = rand.nextInt(GRID_HEIGHT);
    		randomPoint = new Point(randomX,randomY);
    	}
     
    	fruit = randomPoint;
    }
     
    @Override
    public void run() {
    while (true) {
     
    	move();
    	Draw(globalGraphics);
    	try {
    		Thread.currentThread();
    		Thread.sleep(100);
    	}catch (Exception e) {
    		e.printStackTrace();
    	}
    }
     
    }
     
    @Override
    public void keyPressed(KeyEvent e) {
    	switch(e.getKeyCode()) {
    	case KeyEvent.VK_UP:
    		if (direction != Direction.SOUTH)
    		direction = Direction.NORTH;
    		break;
     
    	case KeyEvent.VK_DOWN:
    		if (direction != Direction.NORTH)
    		direction = Direction.SOUTH;
    		break;
     
    	case KeyEvent.VK_LEFT:
    		if (direction != Direction.EAST)
    		direction = Direction.WEST;
    		break;
     
    	case KeyEvent.VK_RIGHT:
    		if (direction != Direction.WEST)
    		direction = Direction.EAST;
    		break;
    	}
    }
     
    @Override
    public void keyReleased(KeyEvent arg0) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void keyTyped(KeyEvent arg0) {
    	// TODO Auto-generated method stub
     
    }
     
     
     
    }

    Direction.java :

    public class Direction {
    public static final int NO_DIRECTION = 0;
    public static final int NORTH = 1;
    public static final int SOUTH = 2;
    public static final int WEST = 3;
    public static final int EAST = 4;
    }


    TyleType.java



    public class TyleType {
     
    	public static final int FRUIT = 2;
    	public static final int SNAKE_BODY = 1;
    	public static final int EMPTY = 0;
     
     
     
     
    }

    snakeApplet.java

    import java.applet.Applet;
    import java.awt.Dimension;
    import java.awt.Graphics;
     
     
    public class snakeApplet extends Applet {
     
    	private snakeCanvas c;
     
    	public void init() {
     
    		c = new snakeCanvas();
    		c.setVisible(true);
    		c.setFocusable(true);
    		c.setSize(new Dimension(640,480));
    		this.add(c);
    		this.setVisible(true);
    		this.setSize(new Dimension(640,480));
     
     
    	}
     
    	public void paint(Graphics g) {
    		this.setSize(new Dimension(640,480));
     
    	}
     
     
    }

    Thanks for help P.S snake in eclipse works perfectly


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Export to jar..

    Refer to this tutorial. There are considerations when using Eclipse's export to jar feature that you have to be aware of and learn to do correctly. For that reason, many recommend using the command line to create .jars. If you ignore that advice, then be sure to test the jar Eclipse creates outside Eclipse, on other platforms if possible, before distributing it widely.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Export to jar..

    Its not help..

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Export to jar..

    What don't you understand? What do you need help with?

    Considering your original questions:

    main() method required? - Since your application appears to be an Applet, you should refer to Deploying an Applet.

    export multiple classes to .jar? - As one project or individually. This topic was covered specifically in the subsequent pages of the link I provided. That it was of no help to you suggests you either didn't read it or didn't understand it. We need to know which is the case to provide you with further help.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Export to jar..

    Yes, you right.. I dont understeand, where write code: jar cvf Snake.jar snakeCanvas.class snakeApplet.class TyleType.class Direction.class

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Export to jar..

    Are you saying you don't know where to type/execute that command? If so, are you familiar with opening a CMD window and using a command prompt in your operating system (OS)? What OS are you using?

    If that's not what you meant to say, then please try to state what you're asking more clearly.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Export to jar..

    Sorry me for stupid question. Java is so big. So i learn how much can... My OS windows xp.. Last question. I understeand, applet is only for web yes? So if i wanna this snake play like desktop aplication, its must be JFrame?

    Thanks for patience

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Export to jar..

    Not stupid questions. There's a challenge with communicating, but you're doing fine.

    Sharing your game with others would be less risky if you were to add code that allows it to appear in a JFrame. You can leave the Applet features and add a main() method that will give the option to run as a Java desktop app. First try to do that, but if you'd like help with it, let us know.

    I may have asked this a long time ago, but why are you using the older Applet class instead of JApplet?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Export to jar..

    With many applets, the code does not need to be changed much to be able to execute it as a desktop app and as an applet in an html page. If the applet doesn't use any AppletContext methods, it often can be added to a container in a JFrame just like any other Panel object. (JApplet extends Panel)
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Export to jar..

    Thanks all for help GregBrannon, i dunno

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Export to jar..

    The graphics in your program are very jerky on my system, or flashing in and out. Studying your code, I see you've done most of the things we advised you not to do when you asked us for advice on this project:

    Overriding the paint() method
    Using Applet rather than JApplet
    (Using AWT rather than Swing)
    Animating with a while(true) loop rather than a Timer

    You can say, "But my way works," and I would answer, "Not very well." I don't remember what you were building this application for, but if you have the time and desire to continue to learn from this project, I suggest you take the time and incorporate the advice you were given. It'll be a worthwhile experience, and you should benefit from it.

    --- Update ---

    This is an outline of a simple class that could be used to run an application either as an applet in a web page (or applet viewer) or as a Java application on the EDT:
    // a class to run the snake game either as a
    // JApplet or as a Java application
    public class SnakeJApplet extends JApplet
    {
        public void init()
        {
            // add contenct
     
            setSize(new Dimension(400,400));
     
        } // end method init()
     
        // a main() method to run the game as application
        // on the EDT
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    // initialize the JFrame
     
                    // add content
     
                    // size it and make it visible
                    gameFrame.pack();
                    gameFrame.setVisible( true );
     
                } // end method run()
     
            } );
     
        } // end method main()
     
    } // end class SnakeJApplet

Similar Threads

  1. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  2. I cant export my applet into a runnable jar file
    By Shef012 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2013, 02:08 PM
  3. Need help to export .jar file
    By piulitza in forum AWT / Java Swing
    Replies: 4
    Last Post: June 8th, 2012, 11:34 AM
  4. [SOLVED] jar file Built(clean and build) perfectly, but not running as jar
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 11th, 2011, 11:41 AM
  5. Export to excel
    By ebosysindia in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: May 14th, 2009, 06:25 AM