gpt4 book ai didi

java - Launch4j 将 jar 转为 exe

转载 作者:行者123 更新时间:2023-11-30 03:06:10 27 4
gpt4 key购买 nike

似乎无法弄清楚为什么当我运行由 launch4j 创建的 .exe 文件时,我的 JFileChooser 从未出现。很多人主要讨论这个错误的类路径,但我没有意识到解决方案。

错误1:

no main manifest attribute in C:\Users\Documents\...\HPLCData.exe

现在,如果我将类路径更改为 KFile.Main,则会收到此错误消息

错误2:

Error: Could not find or locate main class KFile.Main

错误3:

错误:无法找到或定位主类 KFile.class

诚然,我对java相当陌生,也许答案就在我面前或者是显而易见的,如果是这样,抱歉。否则,我读到的内容表明类路径存在问题,并且我不完全确定需要更改什么。

我想知道它是否与生成的 xml 文件中的部分有关,但我不知道那里会发生什么。我尝试过改变所有有点奇怪的事情,closest post我发现提到了一些关于使用完整类路径的内容。或者 jar 包装可能有问题,但我不这么认为,因为我仔细检查了 this

以下是由 launch4j 生成的用于包装 .exe 文件的 .xml 文件。

案例1:

<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>console</headerType>
<jar>C:\Users\red\Documents\Java Modules\Introductory Files\HPLCData.jar</jar>
<outfile>C:\Users\red\Documents\Java Modules\Introductory Files\HPLCData.exe</outfile>
<errTitle></errTitle>
<cmdLine></cmdLine>
<chdir>.</chdir>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<supportUrl></supportUrl>
<stayAlive>false</stayAlive>
<restartOnCrash>false</restartOnCrash>
<manifest></manifest>
<icon></icon>
<jre>
<path>C:\Users\red\Documents\Java Modules\Introductory Files\</path>
<bundledJre64Bit>false</bundledJre64Bit>
<bundledJreAsFallback>false</bundledJreAsFallback>
<minVersion>1.6.0_1</minVersion>
<maxVersion></maxVersion>
<jdkPreference>preferJre</jdkPreference>
<runtimeBits>64/32</runtimeBits>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>version 1</txtFileVersion>
<fileDescription>Manage HPLC Data</fileDescription>
<copyright>Kemin 2016</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>version 1</txtProductVersion>
<productName>HPLC Data</productName>
<companyName>Red</companyName>
<internalName>Red</internalName>
<originalFilename>HPLCData.exe</originalFilename>
</versionInfo>
</launch4jConfig>

案例2:

<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>console</headerType>
<jar>C:\Users\red\Documents\Java Modules\Introductory Files\HPLCData.jar</jar>
<outfile>C:\Users\red\Documents\Java Modules\Introductory Files\HPLCData.exe</outfile>
<errTitle></errTitle>
<cmdLine></cmdLine>
<chdir>.</chdir>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<supportUrl></supportUrl>
<stayAlive>false</stayAlive>
<restartOnCrash>false</restartOnCrash>
<manifest></manifest>
<icon></icon>
<classPath>
<mainClass>KFile.Main</mainClass>
</classPath>
<jre>
<path>C:\Users\red\Documents\Java Modules\Introductory Files\</path>
<bundledJre64Bit>false</bundledJre64Bit>
<bundledJreAsFallback>false</bundledJreAsFallback>
<minVersion>1.6.0_1</minVersion>
<maxVersion></maxVersion>
<jdkPreference>preferJre</jdkPreference>
<runtimeBits>64/32</runtimeBits>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>version 1</txtFileVersion>
<fileDescription>Manage HPLC Data</fileDescription>
<copyright>Kemin 2016</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>version 1</txtProductVersion>
<productName>HPLC Data</productName>
<companyName>Red</companyName>
<internalName>Red</internalName>
<originalFilename>HPLCData.exe</originalFilename>
</versionInfo>
</launch4jConfig>

案例3:

 <manifest></manifest>
<icon></icon>
<classPath>
<mainClass>KFile.class</mainClass>
</classPath>

Java代码:

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.FileVisitResult;
import java.nio.MappedByteBuffer;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.util.Collection;
import java.util.ArrayList;
import java.nio.file.SimpleFileVisitor;



public class KFile extends SimpleFileVisitor<Path> {

public static void main(String[] args) {

Path currPath = Paths.get("");
String currDir = currPath.toAbsolutePath().toString();
System.out.println(currDir);


File dataDir = chooseDir("open");
File destDir = chooseDir("save");


if(!destDir.exists()) {

try {
destDir.mkdir();
}
catch (SecurityException se) {
System.out.println("Couldn't make directory!");
}

}
int n = 0;
if(dataDir.exists()) {
Collection<Path> allDir = new ArrayList<Path>();
try {
addTree(dataDir.toPath(),allDir);
}
catch (IOException e) {
System.out.println("Error with scanning");
}
for( Path thisPath : allDir ) {
if(thisPath.toString().contains("Report.pdf")) {
Path thisDir = thisPath.getParent();
File f = new File(thisDir.toString(), "\\Report.txt");
n = n + 1;
String fileName = "Report " + n + ".pdf";
try {
fileName = parseName(f);
System.out.println(fileName);
} catch (IOException e) {
e.printStackTrace();
}
File thisFile = new File(destDir + "\\" + fileName);

try {
copyFile(thisPath.toFile(),thisFile);
} catch ( IOException e) {
e.printStackTrace();
}
}
}

}


}


public static boolean copyFile(File sourceFile, File destFile) throws IOException {
//create file if it doesn't exist.
if(!destFile.exists()) {
destFile.createNewFile();
}

FileChannel source = null;
FileChannel destination = null;

try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
return true;
}
return false;
}

}

public static File chooseDir(String s) {

JFrame myFrame = new JFrame("HPLC Data Transfer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);

JFileChooser chooser = new JFileChooser();
File currDir = new File(System.getProperty("user.home") + "\\Documents");

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setCurrentDirectory(currDir);

int choice = 0;
if (s.equals("save")) {
choice = chooser.showSaveDialog(myFrame);
} else {
choice = chooser.showOpenDialog(myFrame);
}

myFrame.setVisible(false);
myFrame.removeAll();
myFrame.dispose();
if(choice == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open: " + chooser.getSelectedFile().getName());
return chooser.getSelectedFile();
}
return new File("");
}

static String parseName(File f) throws IOException {

BufferedReader textReader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-16"));

int lnCnt = 32;
String[] fileData = new String[lnCnt];

for (int i = 0; i < lnCnt; i++) {
fileData[i] = textReader.readLine();
}

String name = fileData[1].substring(13) + ".pdf";

textReader.close();
return name;
}

static void addTree(Path directory, final Collection<Path> all)
throws IOException {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
all.add(file);
return FileVisitResult.CONTINUE;
}
});
}

}

最佳答案

如消息所示,Launch4J 似乎调用配置中指定为 mainClass 的任何类的 Main 方法。所以:

  • 对于KFile.class,您需要在package KFile 中有一个名为class 的类。这甚至是不可能的,因为你不能有一个名为 classclass;是保留字

  • 对于KFile.Main,您需要在package KFile中有一个名为Main的类。

您没有包声明,您的类KFile位于默认包中。所以你需要的声明是

<mainClass>KFile</mainClass>

关于java - Launch4j 将 jar 转为 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34728157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com