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:
- Do I need to use JFrame + JPanel?
- Do I need to use JApplet?
- Do I need to use Applet?
- Can I use more than one method?
- 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.
Re: Extreme frustration with JAR files
Quote:
Originally Posted by
meesterpickles
Do I need to use JFrame + JPanel?
Yes, I'd use one or more JPanels for this.
Quote:
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.
Quote:
Do I need to use Applet?
Not unless you are required to use AWT rather than Swing.
Quote:
Can I use more than one method?
Sure, why not?
Quote:
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.
Quote:
An example with source-code would be much appreciated too.
The tutorial has this for you.
Re: Extreme frustration with JAR files
I meant "method" as in a way of doing something, not like a public void init() method XD
Quote:
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:
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.
Re: Extreme frustration with JAR files
Hm Eclipse makes jar files fine for me, so I don't know what you're doing wrong.
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
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
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
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?
Re: Extreme frustration with JAR files
That code has no class that extends JApplet.
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 :(
Re: Extreme frustration with JAR files
Quote:
Originally Posted by
meesterpickles
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).
Re: Extreme frustration with JAR files
Okay, with the help of a tutorial I was able to make this:
Class Game
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
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 :)
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:
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.
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.
Re: Extreme frustration with JAR files
Quote:
Originally Posted by
meesterpickles
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?
1 Attachment(s)
Re: Extreme frustration with JAR files
Okay, here is the contents of the "corrupted" JAR I made in command prompt:
Attachment 2776
Inside of GameManifest.MF:
Quote:
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:
Quote:
Manifest-Version: 1.0
Created-By: 1.6.0_24 (Sun Microsystems Inc.)
Main-Class: Game
Anything wrong there?
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?
Re: Extreme frustration with JAR files
Here is the .zip via mediafire
Here is my html file:
Quote:
<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.
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.
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.
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:
Quote:
Main.java:16: cannot find symbol
symbol : class Panel
location: class strobePackage.Main
getContentPane().add(new Panel());
1 error
Re: Extreme frustration with JAR files
Are both classes in the same package? Do you have package statements at the top of both files?
Re: Extreme frustration with JAR files
Yep, checked and double-checked.