In order to learn how to use jpackage, I have written a small sample application that contains a Swing UI and is supposed to write a log file (this log file only serves as an example for writing to a file that is in the project). First I generate a .jar file, then I package an .exe file with jpackage and then install by running the .exe file. But when I try to start the application, it does not run.
I suspect that the problem is in the log file, as I have tried it without a log file and in this case the whole procedure works fine. I would be very happy to get ideas on how to fix this problem.
So here is the whole procedure in detail
- the java-source-codes:
public class Main {
public static void main(String[] args) {
Log.openLogFile();
try {
new MainFrame("Test-Frame");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
Log.error("Problem while launching the app");
e.printStackTrace();
}
}
}
public class MainFrame extends JFrame{
private static final long serialVersionUID = 1L;
public MainFrame(String name) {
super(name);
Log.tracing("starting with MainFrame");
this.setSize(new Dimension(200,200));
try {
URL url = getClass().getResource("starter.png");
ImageIcon img = new ImageIcon(url);
this.setIconImage(img.getImage());
} catch (Exception e) {
Log.error("couldn't find starter-icon");
e.printStackTrace();
}
JLabel label = new JLabel("Hello");
this.add(label);
this.setVisible(true);
}
}
public class Log {
private static OutputStream os;
private static PrintWriter writer;
public static void warning(String text) {
System.out.println(getTimestamp() + " [w] " + text);
writer.println(getTimestamp() + " [w] " + text);
}
public static void error(String text) {
System.out.println(getTimestamp() + " [e] " + text);
writer.println(getTimestamp() + " [e] " + text);
}
public static void tracing(String text) {
System.out.println(getTimestamp() + " [t] " + text);
writer.println(getTimestamp() + " [t] " + text);
}
private static String getTimestamp() {
String timeStamp = "[" + new SimpleDateFormat("yy_MM_dd HH:mm:ss").format(new Date()) + "]";
return timeStamp;
}
public static void openLogFile() {
try {
File logfile = new File("logging.txt");
logfile.createNewFile();
os = new FileOutputStream(logfile.getPath(), false);
writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
Log.tracing("The logfile is open.");
}
public static void closeLogFile() {
try {
Log.tracing("The logfile is closed.");
writer.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Next, I generated a runnable-jar with eclispe, which ends up in a build-folder on my desktop.
In the next step I use the following command within the windows prompt to pack the project:
C:\Users\xyzUser\Desktop\BuildHello> jpackage --name TestHelloApp --input C:\Users\xyzUser\Desktop\BuildHello\build --main-jar testhello.jar
So I get the installation programme TestHelloApp-1.0.exe.
Now I run this installer and the programme is installed under C:\Programmes\TestHelloApp.
Lastly, I tried to start the programme but it did not run.