简体   繁体   中英

Launch4j for jar to exe

Can't seem to figure out why I my JFileChooser never shows up when I run my .exe file created by launch4j. A lot of folks discuss mostly the class path on this error, but I'm failing to realize the solution.

Error 1:

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

Now if I change the class path to KFile.Main, then I get this error message

Error 2:

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

Error 3:

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

Admittedly, I'm rather new to java and maybe the answer is staring me in the face or is obvious, sorry if so. Otherwise, things I read point to an issue with the class path and I'm not entirely sure what I need to change.

Part of me wonders if it has something to do with the part in the xml file generated, but I have no idea what would go there. I've tried changing all kinda of strange things, the 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

Here are the .xml files generated by launch4j to wrap the .exe file.

case 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>

Case 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>

case 3:

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

Java code:

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;
        }
    });
}

}

As shows in the messages, Launch4J seems to call the Main method of whatever class is specified as mainClass in the configuration. So:

  • For KFile.class you need to have a class named class in package KFile . This is not even possible, since you cannot have a class named class ; is a reserved word

  • For KFile.Main you need to have a class named Main in package KFile .

You do not have a package declaration, your class KFile is in the default package. So the declaration you need is

<mainClass>KFile</mainClass>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM