0

I try to create a simple executable jar file. My steps are:

Test.java

public class Test{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

Terminal:

javac Test.java

produces Test.class

Checking:

java Test  
Hello World

creating a manifest.mf file:

Main-Class: Test

Then

jar -cfmv Test.jar manifest.mf Test.class   
added manifest  
adding: Test.class(in = 413) (out= 287)(deflated 30%)

Finally

java -jar Test.jar  
no main manifest attribute, in Test.jar

I followed all the steps from Katanas answer here: How to create a .jar file using the terminal

Update:

As adviced by ScaryWombat, I unzipped the jar file:

ls -alR  
.:  
total 16
drwxrwxr-x 3 infiniteZero infiniteZero 4096 May 28 02:25 .
drwxrwxr-x 4 infiniteZero infiniteZero 4096 May 28 02:26 ..
drwxrwxr-x 2 infiniteZero infiniteZero 4096 May 28 02:19 META-INF
-rw-rw-r-- 1 infiniteZero infiniteZero  413 May 28 01:50 Test.class

./META-INF:
total 12
drwxrwxr-x 2 infiniteZero infiniteZero 4096 May 28 02:19 .
drwxrwxr-x 3 infiniteZero infiniteZero 4096 May 28 02:25 ..
-rw-rw-r-- 1 infiniteZero infiniteZero   94 May 28 02:19 MANIFEST.MF

cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Main-Class: Test.class
Created-By: 9-internal (Oracle Corporation)
infinitezero
  • 1,610
  • 3
  • 14
  • 29
  • 1
    https://stackoverflow.com/a/18562277/2310289 – Scary Wombat May 28 '18 at 00:17
  • Thanks, I've added a new line, but that does not help, i.e. I get the same error message. – infinitezero May 28 '18 at 00:19
  • a jar file is basically a zip file so rename it and open it in your favourite zip client. Is the contents as expected? – Scary Wombat May 28 '18 at 00:22
  • @ScaryWombat I updated the post with the information. I think it looks like it's supposed to be. Furthermore I tried switching the extension but it still throws the same error. – infinitezero May 28 '18 at 00:32
  • On further inspectation, it seems to add a .class to the Manifest Entry – infinitezero May 28 '18 at 00:34
  • Yeah, that's quite strange. If what is above is true, then it looks like the MANIFEST.MF file is inside META-INF inside the .jar, where it should be. The weirdest part is that this works for me. I'll keep on looking... Make absolutely sure that newline is there, when I tried this I got the same result as you before I added the newline. It might not be saving or something. – Zimri Leisher May 28 '18 at 00:35
  • Nevermind. That was still there from my other attempts. It just says "Test" now, but I still get the same error. Could it be something about my java version or anything? – infinitezero May 28 '18 at 00:37
  • Clarify, what just says "Test" now? – Zimri Leisher May 28 '18 at 00:39
  • When I unpack the jar file and look in the manifest file, there is the entry Main-Class: Test – infinitezero May 28 '18 at 00:41
  • Alright, that's weird. If there's no newline after that then I'm not sure what's going on; I am fairly certain however that it isn't your java version. I don't think the requirements for a jar manifest have changed at all in a while. There's probably a small mistake that you're overlooking because it seems so ridiculous, that's what I've learned is usually the problem with things like this :) – Zimri Leisher May 28 '18 at 00:43
  • better check that the `jar` command and `javac` command are both java9 – Scary Wombat May 28 '18 at 00:49

1 Answers1

1

add an empty line at the end of your manifest, recompile and run. And the manifest you are feeding the compiler hast to be in UTF-8

or:

  • open the jar location in total commander
  • click jar file and press Ctrl + PageDown
  • META-INF -> right click manifest and hit edit
  • add an empty line at the end of your manifest
  • save/exit and run java -jar Test.jar