1

I have scoured Google and StackOverflow for a resolution and found none so far. I am trying to produce a simple executable .jar package using a manifest file from the Windows command line using Java 1.8. The relevant files are:

Turtlephilia.java:

public class Turtlephilia {
    public static void main(String[] args) {
        System.out.println("i lurrrve turtles");
    }
}

manifest.mf:

Main-Class: Turtlephilia

do.bat:

javac Turtlephilia.java

jar cvmf manifest.mf Turtlephilia.jar Turtlephilia.class 

java -jar Turtlephilia.jar

When I execute do.bat it compiles and archives successfully, but upon running the jar file, it generates this error:

no main manifest attribute, in Turtlephilia.jar

NOTE: I am actually able to successfully run it with this command:

java -cp Turtlephilia.jar Turtlephilia

But I need it to be an executable jar.

What am I doing wrong? How hard can it be!?

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
  • Did you happen to create your manifest.mf using Notepad? I suspect there is an invisible [byte-order mark](https://en.wikipedia.org/wiki/Byte_order_mark) (BOM) character at the beginning, which means the line does not technically start with `Main-Class`. Solution: Add a blank line above `Main-Class`. – VGR Mar 28 '16 at 13:25
  • I did edit in notepad, but after adding a blank line I get the same problem. Incidentally something I notice is that if I drag the .jar into Netbeans and take a look, then in the package under META-INF\MANIFEST.ms it has the default manifest with Manifest-Version: 1.0, Created-By: ...., without my Main-Class line. Is it just ignoring my manifest file altogether? – Brendan Hill Mar 28 '16 at 13:38
  • Oh man. You were on the right track. Main-Class was the last line in the manifest file and it needed a carriage return after it. – Brendan Hill Mar 28 '16 at 13:44

1 Answers1

2

Well I found the reason.

I had created the manifest.mf file in Notepad and had a single line for Main-Class.

Turns out that it needs a carriage return after this line to recognize it ie. a blank line at the end.

Having been banging my head against the wall for several hours of this I am not enthralled by the Java development environment.

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
  • Notepad is not a source code editor. Using Notepad and blaming Java is like browsing the web with `wget` and blaming StackOverflow for being hard to use. – dimo414 Mar 29 '16 at 12:57
  • 1
    I disagree. Being incapable of handling the presence/absence of a trailing space is pretty bad, and failing to generate any useful warning when failing compounds the badness. Anyway, do IDE's routinely add trailing spaces to text files? I assume not, so why would they be any different? – Brendan Hill Apr 25 '16 at 00:04
  • Generally I would agree with you; programs should fail gracefully. But there a limits to that gracefulness - you cannot expect programs to always behave reasonably in the face of [garbage input](https://en.wikiquote.org/wiki/Charles_Babbage#Passages_from_the_Life_of_a_Philosopher_.281864.29). And your assumption, while reasonable, is incorrect. Text files [are expected to terminate in a new line](http://unix.stackexchange.com/q/18743/19157) ([background](http://stackoverflow.com/q/729692/113632)), and Notepad failing to add a new line at the end of the file is poor behavior on its part. – dimo414 Apr 25 '16 at 03:23
  • 1
    This bug is still present. Jar.exe could have just validated that my manifest file is correct or not. I also spent several hours on it. There is either no error message or error message is cryptic. I was not expecting such low quality command line tools from Oracle. Such a sad state of basic java tools is probably also reason why there so much insanely complicated build tools - could have been just running few exe files :D – user712092 May 15 '22 at 14:45