Launch4j for jar to exe

时间:2016-01-11 18:04:51

标签: 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:

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

不可否认,我对java很新,也许答案就是盯着我看,或者显而易见,如果是的话,对不起。否则,我读到的内容指出了类路径的问题,而且我并不完全确定需要更改的内容。

我的一部分想知道它是否与生成的xml文件中的部分有关,但我不知道会发生什么。我尝试过更改各种奇怪的东西,closest post我发现曾提到过使用完整的类路径。或者maaybe罐子包装有问题,但我不这么认为,因为我仔细检查了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;
        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

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

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

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

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

<mainClass>KFile</mainClass>