Reputation: 507
As a note, I am no Java Developer but I have limited knowledge from High School.
I was asked to make a simple program just to generate a report and then view it, et cetera (which worked).
I am ecstatic that it works but unfortunately I cannot get the .jar
file to run once I have made it executable I am presuming that it is due to the manifest file not calling exactly what is needed.
I have looked online for help but cannot seem to grasp the concept.
Main class:
package writeBatchFile;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class mainFile {
public static void main(String[] args) {
JFrame myFrame = new JFrame("youwillneverfigurethisout");
myFrame.setLayout(new GridLayout(4, 4));
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton oneDayReport = new JButton("Create Day Report");
JButton weekReport = new JButton("Create Week Report");
JButton monthReport = new JButton("Create Month Report");
JButton runBatch = new JButton("Run Batch File");
JButton viewReport = new JButton("Open Report");
JButton exit = new JButton("Exit");
oneDayReport.setPreferredSize(new Dimension(100, 100));
JLabel label = new JLabel("Please Click On Create Batch ");
JLabel blank = new JLabel("File and then Run Batch File");
myFrame.add(label);
myFrame.add(blank);
myFrame.add(oneDayReport);
myFrame.add(weekReport);
myFrame.add(monthReport);
myFrame.add(runBatch);
myFrame.add(viewReport);
myFrame.add(exit);
myFrame.setVisible(true);
myFrame.setTitle("Report Generator");
myFrame.setSize(400, 200);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
oneDayReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent write) {
try {
String input1 = JOptionPane.showInputDialog("Please Input the Date for the report (ie: 04/07/2014-04/07/2014)");
String input2 = input1;
String line1 = "@echo";
String line2 = "C:\\sarg\\sbin\\sarg –d " + input1 + "-" + input2;
String line3 = "pause";
File file = new File("generateReport.bat");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line1);
bw.write("\r\n \r\n" + line2);
bw.write("\r\n \r\n" + line3);
bw.close();
System.out.println("Done");
} catch(IOException c) {
c.printStackTrace();
}
}
});
weekReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent write) {
try {
String input1 = JOptionPane.showInputDialog("Please a 'from' date for the report (ie: 04/07/2014)");
String input2 = JOptionPane.showInputDialog("Please input a 'to' date for the report (ie: 11/07/2014");
String line1 = "@echo";
String line2 = "C:\\sarg\\sbin\\sarg –d -w " + input1 + "-" + input2;
String line3 = "pause";
File file = new File("generateReport.bat");
// if file doesnt exists, then create it
if ( !file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line1);
bw.write("\r\n \r\n" + line2);
bw.write("\r\n \r\n" + line3);
bw.close();
System.out.println("Done");
} catch (IOException c) {
c.printStackTrace();
}
}
});
monthReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent write) {
try {
String input1 = JOptionPane.showInputDialog("Please a 'from' date for the report (ie: 04/07/2014)");
String input2 = JOptionPane.showInputDialog("Please input a 'to' date for the report (ie: 24/07/2014");
String line1 = "@echo";
String line2 = "C:\\sarg\\sbin\\sarg –d -w -m" + input1 + "-" + input2;
String line3 = "pause";
File file = new File("generateReport.bat");
// if file doesnt exists, then create it
if ( !file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line1);
bw.write("\r\n \r\n" + line2);
bw.write("\r\n \r\n" + line3);
bw.close();
System.out.println("Done");
} catch(IOException c) {
c.printStackTrace();
}
}
});
runBatch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent read) {
try {
Process p = Runtime.getRuntime().exec("cmd /c start generateReport.bat");
p.waitFor();
} catch(IOException ex) {
// Validate the case the file can't be accessed (not enough permissions)
} catch(InterruptedException ex) {
// Validate the case the process is being stopped by some external situation
}
}
});
viewReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent read) {
try {
// using this in real life, you'd probably want to check that the desktop
// methods are supported using isDesktopSupported()...
String htmlFilePath = "test.html"; // path to your new file
File htmlFile = new File(htmlFilePath);
// open the default web browser for the HTML page
Desktop.getDesktop().browse(htmlFile.toURI());
// if a web browser is the default HTML handler, this might work too
Desktop.getDesktop().open(htmlFile);
} catch(IOException ex) {
// Validate the case the file can't be accesed (not enought permissions)
}
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent close) {
System.exit(0);
}
});
}
}
And my manifest.mf looks like such:
Manifest-Version: 1.0
Class-Path: mainFile.class
Main-Class: writeBatchFile.mainFile
As far as I am aware, It is due to the files I am importing such as java.awt
and java.io
but if anyone can write me a manifest file for this or tell me how to create it using either Eclipse or JCreator, I would greatly appreciate it.
Upvotes: 1
Views: 277
Reputation: 507
Thanks all for the help, I was able to fix the error by uninstalling JRE and JDK respectively and then Re-Installing them. The Manifest file was correct
Upvotes: 0
Reputation: 8956
Well I can tell you to create the jar using command prompt.
First as your class contains a package writeBatchFile
, create a folder my the name of writeBatchFile
and keep the java file in that folder.
Now suppose your folder writeBatchFile
exists in G:/
, then open the command prompt in G:/
and type the following command to compile the class
javac writeBatchFile/mainFile.java
Remember you must not navigate inside the package folder. Once compiled type the following command to generate the jar
jar cfve myJar.jar writeBatchFile/mainFile writeBatchFile/*.class
once the jar is generated run the jar by the following command
java -jar myJar.jar
cheers, you are done :)
Upvotes: 2