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?
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>
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>