Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Extreme frustration with JAR files

    I've spent the last week trying to figure out how to do the following:
    Create a simple "game" where the player can move an image around the screen.
    Create an executable Jar file of it.
    Embed it in HTML.

    It may not seem like much, but I've put countless hours into trying to get it to work...

    Initially, I have the following questions for making an Executable JAR file:
    1. Do I need to use JFrame + JPanel?
    2. Do I need to use JApplet?
    3. Do I need to use Applet?
    4. Can I use more than one method?
    5. Is the the format for embedding it in HTML the same for all methods?


    An example with source-code would be much appreciated too.

    It is likely that I'll ask many, many questions in this thread.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Quote Originally Posted by meesterpickles View Post
    Do I need to use JFrame + JPanel?
    Yes, I'd use one or more JPanels for this.

    Do I need to use JApplet?
    This could work well for this type of project. I'd create JPanel(s) and place them in a JApplet.

    Do I need to use Applet?
    Not unless you are required to use AWT rather than Swing.

    Can I use more than one method?
    Sure, why not?

    Is the the format for embedding it in HTML the same for all methods?
    Not sure just what you mean by this. You would read the tutorial on how to create and deply JApplets: Lesson: Java Applets
    and follow the instructions therein.

    An example with source-code would be much appreciated too.
    The tutorial has this for you.

  3. #3
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    I meant "method" as in a way of doing something, not like a public void init() method XD

    Not sure just what you mean by this.
    Is the code for embedding the JAR into HTML the same format in all situations?
    Like this:
    <APPLET ARCHIVE="Test.jar"
    CODE="testPackage.Test.class"
    WIDTH=300
    HEIGHT=300>
    </APPLET>



    Anyways, so I was reading the tutorial and came across this code for making a HelloWorld JApplet:
    Java Code:
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;
    
    public class HelloWorld extends JApplet {
        //Called when this applet is loaded into the browser.
        public void init() {
            //Execute a job on the event-dispatching thread; creating this applet's GUI.
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        JLabel lbl = new JLabel("Hello World");
                        add(lbl);
                    }
                });
            } catch (Exception e) {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    }
    So I tried to make it into a Executable JAR and put it on my page, but Eclipse wouldn't accept it into its Executable JAR Maker and when I tried doing it through the command prompt (with these instructions) I got a broken JAR that couldn't run.

    But it ran on the Oracle webpage fine, so obviously I'm doing something wrong here.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Hm Eclipse makes jar files fine for me, so I don't know what you're doing wrong.

  5. #5
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Okay so I created an executable JAR of the game I described earlier.
    Here is the source: (sorry if this commend is a bit text-heavy)
    Class Test
    Java Code:
    package testPack;
    
    import java.awt.*; 
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JApplet;
    import javax.swing.JFrame; 
    
    /** 
     * Main class for the game 
     */ 
    public class Test extends JFrame
    {        
    	boolean isRunning = true;
    	int windowWidth = 300;
    	int windowHeight = 300;
    	int fps = 30;
    
    	BufferedImage backBuffer;
    	
    	Player p;
    	Keyboard kb;
    	
    	
            public static void main(String[] args) 
            { 
                    Test t = new Test();               
    				t.run();
    				System.exit(0);
            } 
            
            /** 
             * This method starts the game and runs it in a loop 
             */ 
            public void run() 
            { 
            	init();
            	
                    while(isRunning)
                    {
                    	long time = System.currentTimeMillis(); 
                    	
                    	
                    	
                    	
                    	
                    	update();
                    	draw();
                    	
                    	
                    	time = (1000 / fps) - (System.currentTimeMillis() - time);
                    	
                    	
                    	if(time > 0)
                    	{
                    		try
                    		{
                    			Thread.sleep(time);
                    		}
                    		catch(Exception e){}
                    	}
                    }
                    
                    
                    setVisible(false);
            } 
            
            /** 
             * This method will set up everything need for the game to run 
             */ 
            public void init() 
            { 
            	
            	setTitle("Game Tutorial"); 
                setSize(windowWidth, windowHeight); 
                setResizable(false); 
                setDefaultCloseOperation(EXIT_ON_CLOSE); 
                setVisible(true);   
                
                try {
    				p = new Player(200, 200, 6, ImageIO.read(getClass().getResource("player_back.gif")), ImageIO.read(getClass().getResource("player_indifferent_3.gif")), ImageIO.read(getClass().getResource("player_indifferent_4.gif")), ImageIO.read(getClass().getResource("player_indifferent_0.gif")), ImageIO.read(getClass().getResource("player_indifferent_1.gif")), ImageIO.read(getClass().getResource("player_indifferent_2.gif")));
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
                backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
                
                
                kb = new Keyboard();
                addKeyListener(kb);
            } 
            
            /** 
             * This method will check for input, move things 
             * around and check for win conditions, etc 
             */ 
            void update() 
            { 
                    p.anime(kb.left, kb.right, kb.up, kb.down);
                    
            } 
            
            /** 
             * This method will draw everything 
             */ 
            void draw() 
            { 
            	
            	
            	Graphics g = getGraphics(); 
            	Graphics bbg = backBuffer.getGraphics();
            	g.drawImage(backBuffer, 0, 0, this);
            	g.setColor(Color.WHITE); 
            	g.fillRect(0, 0, windowWidth, windowHeight); 
    
            	
            	
            	
            	g.drawImage(p.current, p.x, p.y, this);
            	
            	
            	
            } 
    }
    Class Player
    Java Code:
    package testPack;
    
    import java.awt.Image;
    import java.awt.Insets;
    
    public class Player {
    
    	int x;
    	int y;
    	int speed;
    	
    	
    	Image back;
    	Image front;
    	Image bottomLeft;
    	Image bottomRight;
    	Image left;
    	Image right;
    	Image current;
    
    	
    	public Player(int x, int y, int speed, Image back, Image right, Image bottomRight, Image front, Image bottomLeft, Image left)
    	{
    		this.x = x;
    		this.y = y;
    		this.speed = speed;
    		
    		this.back = back;
    		this.bottomRight = bottomRight;
    		this.right = right;
    		this.front = front;
    		this.bottomLeft = bottomLeft;
    		this.left = left;
    		this.current = front;
    		
    	}
    	
    	public void anime(boolean Left, boolean Right, boolean Up, boolean Down)
    	{
    		
    		if(Left)
    		{
    			x -= speed;
    			current = left;
    		}
    		
    		if(Right)
    		{
    			x += speed;
    			current = right;
    		}
    		
    		if(Up)
    		{
    			y -= speed;
    			current = back;
    		}
    		
    		if(Down)
    		{
    			y += speed; 
    			current = front;
    		}
    		
    		if(x < 0)		
    			x = 0;
    		
    		if(y < 0)
    			y = 0;
    		
    		if(y > 260)
    			y = 260;
    		
    		if(x > 260)
    			x = 260;
    		
    		if(Left && Down)
    		{
    			current = bottomLeft;
    		}
    		
    		if(Right && Down)
    		{
    			current = bottomRight;
    		}
    
    	}
    	
    }
    Class Keyboard
    Java Code:
    package testPack;
    
    import java.awt.Component; 
    import java.awt.event.*; 
    
    /** 
     * Makes handling input a lot simpler 
     */ 
    public class Keyboard implements KeyListener 
    {
    
    	
    	boolean up = false;
    	boolean down = false;
    	boolean left = false;
    	boolean right = false;
    	
    	public void keyPressed(KeyEvent evt) {
    		 
    	       int code = evt.getKeyCode(); 
    
    	       if (code == KeyEvent.VK_LEFT) {
    	     
    	          left = true;
    	       }
    	       else if (code == KeyEvent.VK_RIGHT) {  
    	
    	          right = true;
    	       }
    	       else if (code == KeyEvent.VK_DOWN) {
    	     
    	         down = true;
    	       }
    	       else if (code == KeyEvent.VK_UP) {
    	  	     
    		         up = true;
    		       }
    	   
    	   }
    
    	
    	public void keyReleased(KeyEvent evt) {
    		 
    	       int code = evt.getKeyCode(); 
    
    	       if (code == KeyEvent.VK_LEFT) {
    	     
    	          left = false;
    	       }
    	       else if (code == KeyEvent.VK_RIGHT) {  
    	
    	          right = false;
    	       }
    	       else if (code == KeyEvent.VK_DOWN) {
    	     
    	         down = false;
    	       }
    	       else if (code == KeyEvent.VK_UP) {
    	  	     
    		         up = false;
    		       }
    	   
    	   }
    
    	
    	public void keyTyped(KeyEvent e) {
    
    		
    	}        
          
    }


    So I can click on the file and it opens fine.
    But when I try to put it into my webpage with this code:

    <APPLET ARCHIVE="Test.jar"
    CODE="testPack.Test.class"
    WIDTH=300
    HEIGHT=300>
    </APPLET>



    It gets this error:
    java.lang.reflect.InvocationTargetException


    Any ideas?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    So I need to use JApplet because JFrame isn't supported for websites?
    Okay that makes more sense, I thought they were interchangeable to some degree :(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Quote Originally Posted by meesterpickles View Post
    So I need to use JApplet because JFrame isn't supported for websites?
    Okay that makes more sense, I thought they were interchangeable to some degree :(
    This situation is precisely what applets were built for (though from what I've read, they're not all that popular in the real world).

  9. #9
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Okay, with the help of a tutorial I was able to make this:
    Class Game
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    
    public class Game extends JApplet {
    	
    	public void init()
    	{
    	setSize(256, 256);
    	
    	getContentPane().add(new DemoPanel());
    	}
    
    }
    Class DemoPanel
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    
    
    
    public class DemoPanel extends JPanel
    {
    private int red = 0; 
    void DemoPanel()
    {
    	
    }
    public void paintComponent(Graphics page)
    {
    	 super.paintComponent(page);
    	 Color currentColor = new Color(red,0,0); 
    	 setBackground(currentColor); 
    	 red++;
    	 if(red > 255) 
         red = 0;
    	 repaint();
    	 
    }
    
    }
    It creates a red strobe effect and I think the use of JApplet and JPanel is exactly what you described.
    However it won't let me make it an executable... And where would you even put the game loop on this thing?
    Maybe you could try to JAR it, just to make sure it isn't just me :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    I have jar'd your code and it runs fine, so the code looks to be OK (despite calling repaint() from within paintComponent -- something that should never be done).

    I called it from this html:

    Java Code:
    <HTML>
    <BODY>
    <APPLET CODE=yr12/m01/d/Game.class
      ARCHIVE="MyDemoPanel.jar"
      WIDTH=300
      HEIGHT=160>
    </APPLET>
    </BODY>
    </HTML>
    Of course your class's package structure will be different and so this String "yr12/m01/d/" will be different in your html file.

  11. #11
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Ughhh... this settle it, something is wrong my Eclipse or JDK...
    The project wouldn't show up when I pressed the "Launch Configuration" button on Eclipse
    and all I ended up with was a corrupted JAR when I did it with cmd.

    Any suggestions? I guess I could start by re-installing Java.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Quote Originally Posted by meesterpickles View Post
    Ughhh... this settle it, something is wrong my Eclipse or JDK...
    From my own experience, the odds are much greater still that you're doing something wrong, that this is not a problem with Eclipse or your Java installation.

    Have you unzipped the jar file to be sure that both necessary class files are present? When you've tried to run the jar via html, are you seeing any errors in your browser's Java console (don't use Google's Chrome for this)? Are you sure that your html file refers to the proper path of the class files taking packages into consideration? Are you using packages?

  13. #13
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Okay, here is the contents of the "corrupted" JAR I made in command prompt:
    Extreme frustration with JAR files-ert1.png

    Inside of GameManifest.MF:
    Manifest-Version: 1.0
    Main-Class: Game
    Created-By: 1.2 (Sun Microsystems Inc.)

    Inside of META_INF is the MANIFEST.MF of course.
    And here is said file:
    Manifest-Version: 1.0
    Created-By: 1.6.0_24 (Sun Microsystems Inc.)
    Main-Class: Game

    Anything wrong there?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    I don't see anything wrong yet. Can you make a copy of the jar file, change the copies extension to zip, and upload it to the forum? Also, can you post your html file? Also, are you using packages?

  15. #15
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Here is the .zip via mediafire

    Here is my html file:
    <html>
    <head>
    <title>NERDERP</title>
    </head>
    <body bgcolor="Black">


    <h1><FONT COLOR="FF2300">NERDERP... with 1 D!</FONT COLOR></h1>

    <center><img src="http://www.java-forums.org/images/claw.png" alt="I revel in large rumps and I cannot deceive"/>
    <FONT COLOR="FFFFFF"><p>~Site is under construction~</p>

    <p>(insert some anime reference or programming joke no one would get)</p>

    <p><i>Nerderp.com, for all your Java game needs.</i></p>

    <p>DEAN YOU ARE AMAZING I WILL FOREVER BE IN AWE OF YOUR GLORY</p>


    <p><b>IGNORE EVERYTHING DOWN HERE, JAVA TESTING STUFF, AS I SAID IGNORE.</b></p>





    <APPLET

    CODE="Game.class"
    ARCHIVE="Game.jar"
    WIDTH=300
    HEIGHT=300>
    </APPLET>






    </FONT COLOR></center>






    </body>
    </html>

    I'm currently just using the default package.
    Last edited by meesterpickles; 01-30-2012 at 03:23 AM. Reason: forgot something

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Your jar file doesn't work. Try avoiding use of the default package and try putting the code into a package.

  17. #17
    TaintedMike is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    If your using a package, you have to set it in the html code like so:

    package.class1.class
    package.class1.jar

    Folders:
    package folder - store class and jars.
    Main Folder - for the html file.

  18. #18
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    I've added the two classes into a package.
    As usual, it won't let me create an executable JAR, but now, I can't even compile the two classes in the command prompt.

    Note:
    I created a new project with new classes:
    Test.java is now called Main.java.
    DemoPanel.java is now called Panel.java.
    The package they are in is called strobePackage.
    The project is called strobe.

    I get this error when compiling Main.java with the javac command:
    Main.java:16: cannot find symbol
    symbol : class Panel
    location: class strobePackage.Main
    getContentPane().add(new Panel());
    1 error

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Extreme frustration with JAR files

    Are both classes in the same package? Do you have package statements at the top of both files?

  20. #20
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Extreme frustration with JAR files

    Yep, checked and double-checked.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 11
    Last Post: 12-04-2011, 10:10 PM
  2. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  3. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  4. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  5. Extreme nooby having hard time with this app
    By asterix350z in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 07:24 PM

Posting Permissions

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