Sign in to follow this  
SSVicious

Manifest Files for Java .jar Files

Recommended Posts

SSVicious    108
Hi, I just can not create a .jar file for the life of me! Here is my manifest file: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.6.0 Created-By: 1.4.2-b28 (Sun Microsystems Inc.) Main-Class: MainApp The name of my class in my MainApp.java file is MainApp. The MANIFEST.MF file is in a directory called META-INF in the .jar file that I have created. The MainApp.class file is not in a folder, it is just like this: MainApp.jar -MainApp.class -META-INF -MANIFEST.MF This all sounds good, but when I double click on the .jar file, expecting it to auto-run like a .exe file, it says "Fatal exception occured. Program will exit." I've been trying to get a self-executable .jar file for the past two days, and the bloody thing will not work. What am I doing wrong? Please help!

Share this post


Link to post
Share on other sites
Zahlman    1682
Does MainApp contain public static void main(String[])?
Did you test running the class by itself at the command line?

Share this post


Link to post
Share on other sites
SSVicious    108
It did not contain the main() method. I added that. Now it doesn't give me that error, it just doesn't do anything when I double click the .jar file.


Yes, the program works, I've tested the .class file.

Share this post


Link to post
Share on other sites
Matei    190
The public static void main() method should actually run the program. Also, running executable JARs doesn't open a command prompt, so if you're trying to read from System.in or write to System.out, you won't see anything.

Try writing your main() like this to check that it is actually being executed:

public class MainClass {
public static void main(String[] args) {
javax.swing.JOptionPane.showMessageDialog(null, "It Works!");
System.exit(0);
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Whenever I try to call any functions or anything from the main() method, it says I can't call up non-static methods from a static method. So if it is supposed to run the whole program, how can I do that? Here is my code.



import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;



public class MainApp extends Applet implements Runnable, MouseListener, MouseMotionListener
{


//Double Buffer Variables
Image offscreenImage;
Graphics offscr;

int mx;
int my;
Image logo;
Thread t;
int i;

public static void main(String[] args){}


public void init()
{
setSize(806,558);
setBackground(Color.black);
setVisible(true);
offscreenImage= createImage(getSize().width, getSize().height);

offscr = offscreenImage.getGraphics();

logo= getImage(getDocumentBase(),"MenuImage.gif");
t= new Thread(this);
t.start();
i=0;
addMouseListener( this );
addMouseMotionListener( this );

run();
}

public void run()
{
while(true)
{
i++;
repaint();
try
{
t.sleep(1000/33);
}
catch(InterruptedException e){;}
}
}



public void mouseEntered( MouseEvent e ) {

}
public void mouseExited( MouseEvent e ) {

}

public void mouseClicked( MouseEvent e ) {}
public void mousePressed( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ) {}
public void mouseDragged( MouseEvent e ) {}


public void mouseMoved( MouseEvent e ) {
mx = e.getX();
my = e.getY();

repaint();
e.consume();
}


public void paint(Graphics g)
{
offscr.setColor(Color.black);
offscr.fillRect(0,0,getSize().width, getSize().height);
offscr.drawImage(logo, mx, my, this);
g.drawImage(offscreenImage, 0, 0, this);
g.setColor(Color.white);
g.drawString("i = "+i, 10, 500);
}


public void update(Graphics g)
{
paint(g);
}
}

Share this post


Link to post
Share on other sites
Zahlman    1682
Umm. Now I see you problem. Applets have an entirely different 'model' for running than jars or ordinary applications (running the *class* from the command line).

The main() method is going to call either

- other static methods
or, ultimately via those other methods (unless you write in an ungodly ugly, completely procedural style)
- object methods.

Here's a hint. To call an object method, you need to
(1) create the object.
(2) call the appropriate method on the object.

However, before you make the mistake: you don't want to call run() directly on Runnable objects. Instead, make a Thread object which takes your own object as a parameter, and call its start() method. That will do the necessary work behind the scenes to actually create a thread, and then call your object's run() method (which it knows is there because you implement Runnable) in its own thread.

The reason you haven't done any of this stuff before is that the Applet interface handles it all for you. When you're not running in a browser you don't get such luxuries :P

Share this post


Link to post
Share on other sites
aidan_walsh    739
If you're going to be calling methods from the same class within a static method, the method you are trying to call also has to be static. Otherwise, you have to move the methods into a class, create an object of that class type and access them through that.

Share this post


Link to post
Share on other sites
Matei    190
The *only* method called when you double-click your JAR file is main, so if you leave it empty, as you did, nothing will happen :).

You probably want to create a window, add an instance of your applet to that window, init() your applet and start() it. There are already several classes that do this which people have written - try http://www.acme.com/java/software/Acme.MainFrame.html (click "fetch the software", and put the .java file in a folder called Acme right inside your JAR). Then your main() should contain something like new Acme.MainFrame(new MainApp(), args, 400, 400);.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this