2

I'm am very confused with this problem. It didn't happen for the first few times when ran this code, but after that, I couldn't get it running again. It only breaks after downloading it using my Java code. The code I have downloads a jar file from my website and then uses ProcessBuilder to run it. It all worked fine the first couple of times, but I ran it a few more times and it started giving me

no main manifest attribute, in ...\AppData\Local\GDRQ\bin\launch_gdbotbin.jar

Here is the java code I use to download and run the jar file:

try {
        System.out.println("starting update...");

        File targetFile = new File(System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\downloading\\launch_gdbotbin.jar");
        URL downloadUrl = new URL("http://www.dropmoose.com/gdbotpro/launch_gdbotbin.jar");

        InputStream inStream = downloadUrl.openStream();
        //System.out.println(inStream.available());
        BufferedInputStream bufIn = new BufferedInputStream(inStream);
        OutputStream out = new FileOutputStream(targetFile);
        BufferedOutputStream bufOut = new BufferedOutputStream(out);
        byte[] buffer = new byte[inStream.available()];

        while (true) {
            int nRead = bufIn.read(buffer, 0, buffer.length);

            if (nRead <= 0) {
                break;
            }

            bufOut.write(buffer, 0, nRead);
        }

        bufOut.flush();
        out.close();
        inStream.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter(settingsFile));
        for (int i = 0; i < settings.size(); i++) {
            bw.write(settings.get(i));
            bw.newLine();
        }

        bw.close();
        System.out.println("update successful");
        infoPane.dispose();

        System.out.println("cleaning up...");
        Files.move(targetFile.getAbsoluteFile().toPath(), new File(System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\bin\\launch_gdbotbin.jar").toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (Exception e) {
        JOptionPane.showConfirmDialog(null, "An error occured (code 14): Unable to update!");
        infoPane.dispose();
    }

String[] parm = {"jar", "-jar", System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\bin\\launch_gdbotbin.jar"};

ProcessBuilder pb = new ProcessBuilder(parm);
    pb.redirectOutput(Redirect.INHERIT);
    pb.redirectError(Redirect.INHERIT);

    try {
        pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

I tried creating a MANIFEST.MF file with Class-Path as the class with the main(String[] args) method and Manifest-Version: 1.0 but it still didn't work. What am I doing wrong and why is my manifest.mf file not working? The jar that the code downloads is also about 125KB when the original one is 2.5 MB.

EDIT If you go to the url in the code that downloads the jar file and then run it, it runs perfectly fine.

EDIT 2 I even tried going into the downloaded jar file using WinRAR and manually changing the META-INF/MANIFEST.MF file to have the Main-Class set properly, and after doing so, it still didn't run.

EDIT 3 The manifest problem is gone. But now I have a problem with loading referenced libraries. I am using PircBotX and when you run the jar file (using String[] parm = {"java", "launch_gdbotbin.jar", "gd.bot.main.BotHandler"}) it throws ClassNotFoundException.

EDIT 4 Even after I did what Micheal Markidis recommended, it started giving me the no main manifeset attribute error again.

  • What does the parm String look like? – Michael Markidis Apr 25 '16 at 23:24
  • String[] parm = {"jar", "-jar", System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\bin\\launch_gdbotbin.jar"}; – TreeHouseFalcon Apr 25 '16 at 23:24
  • Aren't you trying to run the jar file from java? Wouldn't you use the java command instead? – Michael Markidis Apr 25 '16 at 23:27
  • If I use "java -jar path/to/jar/file", it does the same thing. – TreeHouseFalcon Apr 25 '16 at 23:27
  • http://prntscr.com/awur6i – TreeHouseFalcon Apr 25 '16 at 23:28
  • I would try "java -jar path/to/jar/file path/to/class/ClassName" This is a way of telling java what main class you want to run in the jar. – Michael Markidis Apr 25 '16 at 23:28
  • It still says no main manifest attribute. – TreeHouseFalcon Apr 25 '16 at 23:30
  • @MichaelMarkidis No it isn't. The -jar option names a JAR file, and it assumes there is a Main-class attribute in the manifest. It ignores any class name provided on the command line, and also any CLASSPATH. The command line you suggested will not work. – user207421 Apr 25 '16 at 23:33
  • I removed the -jar from the arguments array, and I got a new error: Could not find or load main class launch_gdbotbin.jar – TreeHouseFalcon Apr 25 '16 at 23:38
  • You definitely need the -jar option. Is there anyway you can recreate the jar file that's hosted on your website. It may just be that you created the jar incorrectly. What options did you use to create it in the first place. – Michael Markidis Apr 25 '16 at 23:48
  • That's probably what it is. So I guess the real question is: how do you PROPERLY download a jar file, its manifest, AND all of its referenced libraries? I looked in the jar file using winrar, and it looks like there are none of the libraries I referenced in eclipse: http://prntscr.com/awuz34 – TreeHouseFalcon Apr 25 '16 at 23:51
  • Re your edit, there is no such exception as `NoSuchClassException`. Do you mean `ClassNotFoundException`? In which case you need to provide a Class-path entry in the manifest naming the other JAR files, *and* you need to download them. Are you perhaps looking for JWS? – user207421 Apr 25 '16 at 23:56
  • No, I am making a java application that downloads an updated version of a jar file every time the user starts the application. – TreeHouseFalcon Apr 26 '16 at 00:04

1 Answers1

0

I can actually get your code to run:

What I changed for my environment:

  1. I took out the code that writes the settings file
  2. I took out the code that does the move

    Files.move(targetFile.getAbsoluteFile().toPath(), new File(System.getProperty("user.home") + "\AppData\Local\GDRQ\bin\launch_gdbotbin.jar").toPath(), StandardCopyOption.REPLACE_EXISTING);

  3. You need to have the

    String[] parm = {"java", "-jar", System.getProperty("user.home") + "path/to/your/file/launch_gdbotbin.jar"};

I get this to come up enter image description here

  1. I would try to replicate what I did and then incrementally put back the other pices of code that I removed.

Note: Just manually delete the launch_gdbotbin.jar each time for now.

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • Awesome. I knew we were really close. Try to post back what the issues were with the conflicting code. That way I can add it to the answer for future views. – Michael Markidis Apr 26 '16 at 00:17
  • I'm pretty sure it was moving the file from the downloading folder to the bin folder. The class probably wasn't found because it was changing at the same time as it was being ran. – TreeHouseFalcon Apr 26 '16 at 00:19
  • That makes sense. I guess the way you are calling Files.move is asynchronous. – Michael Markidis Apr 26 '16 at 00:31
  • I left it alone for about an hour, and when I got back to work on it some more, the missing manifest problem came up again. I did not change any of the code. I made sure that it ran first before changing anything – TreeHouseFalcon Apr 26 '16 at 01:39
  • That's strange. So you have the Files.move part commented out? Something must have changed. – Michael Markidis Apr 26 '16 at 01:54
  • The only thing I did was export it and try and turn it into a .exe file using Launch4J. – TreeHouseFalcon Apr 26 '16 at 01:59
  • This is getting strange... I changed the manifest.mf file I made in my workspace. I changed Class-Path: gd.bot.main.BotHandler to Main-Class: BotHandler and it worked... – TreeHouseFalcon Apr 26 '16 at 02:22
  • I'm actually getting inconsistent behavior now as well. It seems that sometimes the jar fully downloads and sometimes it doesn't. You may need to modify the code that downloads the file to ensure that you have all of it. – Michael Markidis Apr 26 '16 at 02:28