Manifest Files for Java .jar Files

Started by
9 comments, last by Matei 19 years, 9 months ago
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!
Advertisement
Does MainApp contain public static void main(String[])?
Did you test running the class by itself at the command line?
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.
Post the source? What are you expecting the program to do?
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);    }}
is the MANIFEST.MF in META-INF?

anyway I use JarBuilder at sourceforge.net to build my jar
If you use eclipse it's very easy to create excutable jar-files. And it's a great IDE for Java. Justa a tip.

LizardCPP
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);
}
}

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
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.
www.aidanwalsh(.net)(.info)

This topic is closed to new replies.

Advertisement