Launch4j for jar to exe

Launch4j for jar to exe

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

错误 1:

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

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

错误 2:

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

错误 3:

错误:找不到或找不到主要内容 class KFile.class

诚然,我是 java 的新手,也许答案就在眼前,或者很明显,如果是的话,我很抱歉。否则,我读到的东西指向 class 路径的问题,我不完全确定我需要更改什么。

我有点想知道它是否与生成的 xml 文件中的部分有关,但我不知道那里会发生什么。我试过改变各种奇怪的东西, closest post I found had mentioned something about using the full class path. Or maaybe there is a problem with the jar wrapping, but I don't think so as I double checked this

这里是 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>

案例二:

<?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 的任何 class 的 Main 方法。所以:

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

  • 对于 KFile.Main 你需要在 package KFile.

  • 中有一个名为 Main 的 class

您没有包裹声明,您的 class KFile 在默认包裹中。所以你需要的声明是

<mainClass>KFile</mainClass>