0

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

  1. 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();
        }
    }
}

  1. Next, I generated a runnable-jar with eclispe, which ends up in a build-folder on my desktop.

  2. 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.

  1. Now I run this installer and the programme is installed under C:\Programmes\TestHelloApp.

  2. Lastly, I tried to start the programme but it did not run.

  • Why do you say 'it did not run'? Was there an error message? – Queeg Oct 06 '22 at 10:45
  • Most likely a duplicate - see how to view the java console of your generated EXE here: [In Java how do you debug exe created by JPackage](https://stackoverflow.com/questions/62606900/in-java-how-do-you-debug-exe-created-by-jpackage). Hopefully the console will give you enough information to determine the exception / whether you've omitted jars from the packager. – DuncG Oct 06 '22 at 11:15
  • 1
    One possible problem when you start the program in C:\Programmes\TestHelloApp: the folder C:\Programmes and its subfolders are normally only writeable when running with elevated privileges. You could try to use a filename like C:\Users\xyzUser\documents\log.txt – Thomas Kläger Oct 06 '22 at 11:22
  • I think that is exactly the problem. I do not have the right to write to the directory C:\Program Files\. – Asterix37 Oct 09 '22 at 05:32

2 Answers2

1

Errors in the generated / installed executables can be seen if you enable the java console with --win-console. Hopefully the console will give you enough information to determine the exception / whether you've omitted jars from the packager.

On Windows, a more appropriate location for log file that ensures it is writable would be:

 Path logFile = Path.of(System.getenv("LOCALAPPDATA"),"yourappname", "yourlogfilename.log");
 // OR
 Path logFile = Path.of(System.getProperty("java.io.tmpdir"), "yourlogfilename.log");

Depending on your logFile choice above, you may need to create the directory structure before opening the file:

 Files.createDirectories(logFile.getParent());
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • The command --win-console works fine for me. Thank you for this. – Asterix37 Oct 09 '22 at 05:39
  • I have also tried changing the location of the log file as described above, but for some reason the log file is only created but remains empty. – Asterix37 Oct 09 '22 at 05:40
  • `PrintWriter` does not autoflush data. Use `flush()` after each write or use the constructor with the extra parameter `autoFlush` set to true. – DuncG Oct 09 '22 at 07:52
0

Try to place your logfile into a writable location.

File logfile = new File(System.getProperty("user.home") + "/logging.txt");

Also, if you had used one of the available logging libraries (log4j, slf4j, java.util.logging, ...) you would have seen they do not make the application fail just for not being able to write log messages.

At least modify your code so the UI will show a message if it cannot initialize the Log.

Queeg
  • 7,748
  • 1
  • 16
  • 42