JAR problem

F

farsight

Hi, i'm having some problems making a runnable JAR file, and I wasn't
sure where to turn. These are my first steps in Java, so bear with me.

Basically, in the class which holds the main body of the code I have
'extended Applet implements runnable', in my init() method I create
the new thread, and in the run() method I go through an infinite loop
inside which game events happen. When I run the class with eclipse
everything works fine, and I can play through my (terrible) pong game
with no problems.

Obviously the problem here is that there is no main() method in my
program, and there doesn't seem to be space for one. Simply adding and
empty main() method to my SimplePong class lets eclipse export the
project to a .jar file, but the program still can't be run.
Confusingly, eclipse will still run the project regardless of the
inclusion of the main() method.

If anyone could give me some pointers as to what I should put in there
to make the program run correctly, that would be really helpful.
Thanks.
 
F

farsight


Thanks for those links, but I have been trying all afternoon to no
avail. It seems that the wrapper code suggested isn't compatible with
the app the way I wrote it unfortunately. I guess I will have to re-
write it? I looked for another tutorial on creating an application,
but pretty much every command is different. It suggests 'extends
Jpanel implements Actionlistener' instead. So, which method is better?
Will the second method i'm looking into now be capable of running in a
browser?
 
K

Knute Johnson

Thanks for those links, but I have been trying all afternoon to no
avail. It seems that the wrapper code suggested isn't compatible with
the app the way I wrote it unfortunately. I guess I will have to re-
write it? I looked for another tutorial on creating an application,
but pretty much every command is different. It suggests 'extends
Jpanel implements Actionlistener' instead. So, which method is better?
Will the second method i'm looking into now be capable of running in a
browser?

If you are going to make an application I wouldn't use an Applet or
JApplet as my main window. That said, a simple way to make both an
Applet and an application is to create by extending JComponent or JPanel
and then putting that in either a JApplet or a JFrame. It is possible
to make an applet into an application though. The code below will run
as either, maybe you can fix your program to work like this.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements Runnable {
Label l;
Button b;
Thread t;
int n;

public void init() {
l = new Label(" ");
b = new Button("Press Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
t.start();
}
});

add(l);
add(b);

t = new Thread(this);
}

public void stop() {
t.interrupt();
}

public void run() {
try {
while (true) {
Thread.sleep(1000);
l.setText(Integer.toString(n++));
}
} catch (InterruptedException ie) {
System.out.println("thread interrupted");
}
}

public static void main(String[] args) {
final test t = new test();
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
t.stop();
f.dispose();
}
});
t.init();
f.add(t);
f.pack();
f.setVisible(true);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top