Java 启动jar到exe的4J
似乎无法理解为什么在运行launch4j创建的.exe文件时,我的Java 启动jar到exe的4J,java,xml,exe,launch4j,Java,Xml,Exe,Launch4j,似乎无法理解为什么在运行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: 错
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文件中的部分有关,但我不知道会发生什么。我尝试过改变所有奇怪的事情,我发现我提到了一些关于使用完整类路径的事情。或者也许罐子的包装有问题,但我不这么认为,因为我仔细检查了一下
以下是launch4j生成的.xml文件,用于包装.exe文件
案例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>
假的
安慰
C:\Users\red\Documents\Java Modules\introductive Files\HPLCData.jar
C:\Users\red\Documents\Java Modules\introductive Files\HPLCData.exe
.
正常的
http://java.com/download
假的
假的
C:\Users\red\Documents\Java模块\介绍性文件\
假的
假的
1.6.0_1
首选
64/32
1.0.0.0
版本1
管理HPLC数据
科明2016
1.0.0.0
版本1
高效液相色谱数据
红色
红色
HPLCData.exe
案例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>
假的
安慰
C:\Users\red\Documents\Java Modules\introductive Files\HPLCData.jar
C:\Users\red\Documents\Java Modules\introductive Files\HPLCData.exe
.
正常的
http://java.com/download
假的
假的
主文件
C:\Users\red\Documents\Java模块\介绍性文件\
假的
假的
1.6.0_1
首选
64/32
1.0.0.0
版本1
管理HPLC数据
科明2016
1.0.0.0
版本1
高效液相色谱数据
红色
红色
HPLCData.exe
案例3:
<manifest></manifest>
<icon></icon>
<classPath>
<mainClass>KFile.class</mainClass>
</classPath>
KFile.class
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;
}
});
}
}
import java.io.*;
导入java.nio.channels.FileChannel;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.Files;
导入java.nio.file.attribute.BasicFileAttributes;
导入java.nio.file.FileVisitResult;
导入java.nio.MappedByteBuffer;
导入javax.swing.JFileChooser;
导入javax.swing.JFrame;
导入java.util.Collection;
导入java.util.ArrayList;
导入java.nio.file.SimpleFileVisitor;
公共类KFile扩展了SimpleFileVisitor{
公共静态void main(字符串[]args){
Path currPath=Path.get(“”);
字符串currDir=currPath.toabsolutionPath().toString();
系统输出打印项次(currDir);
文件dataDir=chooseDir(“打开”);
文件destDir=chooseDir(“保存”);
如果(!destDir.exists()){
试一试{
destDir.mkdir();
}
捕获(安全异常se){
System.out.println(“无法生成目录!”);
}
}
int n=0;
if(dataDir.exists()){
Collection allDir=new ArrayList();
试一试{
addTree(dataDir.toPath(),allDir);
}
捕获(IOE异常){
System.out.println(“扫描错误”);
}
for(路径thisPath:allDir){
if(thisPath.toString().contains(“Report.pdf”)){
Path thisDir=thisPath.getParent();
文件f=新文件(thisDir.toString(),“\\Report.txt”);
n=n+1;
String fileName=“报告”+n+“.pdf”;
试一试{
fileName=parseName(f);
System.out.println(文件名);
}捕获(IOE异常){
e、 printStackTrace();
}
File thisFile=新文件(destDir+“\\\”+文件名);
试一试{
复制文件(thisPath.toFile(),thisFile);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
}
公共静态布尔copyFile(文件sourceFile、文件destFile)引发IOException{
//如果文件不存在,则创建该文件。
如果(!destFile.exists()){
destFile.createNewFile();
}
filechannelsource=null;
filechanneldestination=null;
试一试{
source=新文件输入流(sourceFile).getChannel();
destination=新文件输出流(destFile).getChannel();
destination.transferFrom(source,0,source.size());
}
最后{
如果(源!=null){
source.close();
}
if(目的地!=null){
destination.close();
返回true;
}
返回false;
}
}
公共静态文件选择dir(字符串s){
JFrame myFrame=新JFrame(“HPLC数据传输”);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
JFileChooser chooser=新的JFileChooser();
File currDir=新文件(System.getProperty(“user.home”)+“\\Documents”);
setFileSelectionMode(仅限于JFileChooser.DIRECTORIES_);
选择器.setCurrentDirectory(currDir);
int-choice=0;
如果(s.等于(“保存”)){
choice=chooser.showsavedilog(myFrame);
}否则{
choice=chooser.showOpenDialog(myFrame);
}
myFrame.setVisible(false);
myFrame.removeAll();
myFrame.dispose();
if(choice==JFileChooser.APPROVE\u选项){
<mainClass>KFile</mainClass>