Program will run with errors, but not at all in a .jar file

First off, here is my program right now:

import java.io.*;

import java.util.*;

import javax.swing.JOptionPane;

publicclass prune

{

publicstaticvoid main(String args[])

{

String steamid="",time="";

BufferedReader infile =null;

BufferedWriter outfile =null;

FileReader fr =null;

FileWriter wr =null;

StringTokenizer strtk =null;

String line =null;

JOptionPane.showMessageDialog

(null,"Vault.ini Pruner v2");

String filepath = JOptionPane.showInputDialog("Enter the filepath to your vault.ini file.");

String strdeletenumber = JOptionPane.showInputDialog("Enter the number that vault entries under will be deleted");

int deletenumber = Integer.parseInt(strdeletenumber);

try

{

infile =new BufferedReader(new FileReader(filepath));

outfile =new BufferedWriter(new FileWriter(filepath));

}

catch(IOException ioe)

{

JOptionPane.showMessageDialog

(null,"Can't open vault.ini:" + ioe);

}

try

{

while((line=infile.readLine())!=null)

{

strtk =new StringTokenizer(line);

steamid = strtk.nextToken();

time = strtk.nextToken();

if(Integer.parseInt(time)>=deletenumber)

{

outfile.write(steamid);

outfile.write(" ");

outfile.write(time);

outfile.newLine();

}

}

}

catch(IOException ioe)

{

JOptionPane.showMessageDialog

(null,"Error:" + ioe);

}

try

{

outfile.close();

infile.close();

}

catch(IOException ioe)

{

JOptionPane.showMessageDialog

(null,"Error:" + ioe);

System.exit(0);

}

}

}

The program is supposed to open a vault.ini file and delete entries with a number lower than specified.

Vault files are set up like this:

STEAMID:X:XXXX 100000

Right now if I run the program through command prompt it erases both the vault.ini and new vault.ini. I am also trying to put it in an executable jar file and when I do that I get a "Failed to load main class manifest attribute" error. Any ideas on what is causing this?

[3944 byte] By [Xenogenetica] at [2007-10-2 19:25:44]
# 1

When you put a program into a jar file and expect it to run you need to make sure that the META-INF/MANIFEST.MF file contains, in your case:

Main-Class: prune

in it. If you are using [url=http://ant.apache.org/]ant[/url] you can include a task as part of the compile that looks like:

<target name="jar" depends="compile">

<jar destfile="${dest.lib.dir}/${dest.lib.name}" basedir="${dest.classes.dir}">

<manifest>

<attribute name="Built-By" value="${user.name}"/>

<attribute name="Main-Class" value="prune" />

</manifest>

</jar>

</target>

stdunbara at 2007-7-13 21:11:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

I have a mainclass.txt that says:

"Main-Class: prune" without quotes and followed by an empty line.

this is the command line I use to compile it to a jar file:

jar cmf mainClass.txt Pruner.jar prune.class

I don't really want to install Ant unless I have to, and I don't think it is required.

Xenogenetica at 2007-7-13 21:11:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

I'm not sure if I understand what you mean about having a mainclass.txt. Within a Jar file you must have a file name META-INF/MANIFEST.MF that contains at least:

Manifest-Version: 1.0

Main-Class: prune

This file must be named as above and yes, it is case sensitive. Take a look at [url=http://java.sun.com/docs/books/tutorial/deployment/jar/basicsindex.html]Using JAR Files[/url] for more information.

stdunbara at 2007-7-13 21:11:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4
I use the 'm' command to modify the manifest file that is automatically generated. To test this I made a simple jar program that prints out "test" and it works. I think the problem is with the prune program itself, but from reading it I think everything should work.
Xenogenetica at 2007-7-13 21:11:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5

I don't know what is happening. I put your exact code into a small build environment and used a build file for ant that I have and it works just fine. Manifest files are a total pain which is why I use a tool to generate it. I know that the last line has to be blank and that no line can be over a certain length.

You've now spent several days avoiding ant and I got it running with ant in about 3 minutes. I'm really missing something.

For reference, the build file is below should you change your mind. Put your prune.java in a new directory named "src" and save this file below as build.xml in the parent directory of "src". Run the program with java -jar lib/prune.jar

<project name="jartest" default="main" basedir=".">

<!-- location properties -->

<property name="src.dir" location="src" />

<property name="dest.classes.dir" location="classes" />

<property name="dest.lib.dir" location="lib" />

<!-- value properties -->

<property name="dest.lib.name" value="prune.jar" />

<property name="main.class" value="prune" />

<!-- compile time value properties -->

<property name="compile.debug" value="true" />

<property name="compile.optimize" value="false" />

<property name="compile.deprecation" value="true" />

<property name="compile.source" value="1.4"/>

<property name="compile.target" value="1.4"/>

<!-- build -->

<target name="main" depends="compile,jar" />

<target name="compile">

<mkdir dir="${dest.classes.dir}"/>

<mkdir dir="${dest.lib.dir}"/>

<javac srcdir="${src.dir}"

destdir="${dest.classes.dir}"

debug="${compile.debug}"

deprecation="${compile.deprecation}"

optimize="${compile.optimize}"

source="${compile.source}"

target="${compile.target}" >

</javac>

</target>

<!-- clean -->

<target name="clean">

<delete dir="${dest.classes.dir}"/>

<delete dir="${dest.lib.dir}"/>

</target>

<!-- jar -->

<target name="jar" depends="compile">

<jar destfile="${dest.lib.dir}/${dest.lib.name}"

basedir="${dest.classes.dir}">

<manifest>

<attribute name="Built-By" value="${user.name}"/>

<attribute name="Main-Class" value="${main.class}" />

</manifest>

</jar>

</target>

</project>

stdunbara at 2007-7-13 21:11:17 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...