为了学习如何使用jpackage,我编写了一个小的示例应用程序,它包含一个Swing UI,并应该编写一个日志文件(这个日志文件仅用作写入项目中的文件的示例)。首先生成一个.jar文件,然后用jpackage打包一个.exe文件,然后通过运行.exe文件进行安装。但是,当我尝试启动应用程序时,它不会运行。
我怀疑问题出在日志文件中,因为我尝试过没有日志文件,在这种情况下,整个过程运行良好。我很乐意就如何解决这个问题提出意见。
下面是详细的整个过程
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();
}
}
}。
在下一步的
C:\Users\xyzUser\Desktop\BuildHello> jpackage --名称TestHelloApp --输入C:\Users\xyzUser\Desktop\BuildHello\TestHelloApp-main-jar testhello.jar
所以我得到了安装程序TestHelloApp-1.0.exe。
现在我运行这个安装程序,程序安装在C:\Programmes\TestHelloApp.下
发布于 2022-10-06 12:24:13
如果使用--win-console启用java控制台,则可以看到生成/安装的可执行文件中的错误。希望控制台将提供足够的信息来确定异常/是否遗漏了包程序中的jars。
在Windows上,确保日志文件可写的更合适的位置是:
Path logFile = Path.of(System.getenv("LOCALAPPDATA"),"yourappname", "yourlogfilename.log");
// OR
Path logFile = Path.of(System.getProperty("java.io.tmpdir"), "yourlogfilename.log");根据上面的logFile选择,您可能需要在打开文件之前创建目录结构:
Files.createDirectories(logFile.getParent());https://stackoverflow.com/questions/73972443
复制相似问题