Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot execute binary file - Executable Jar file

I'm having trouble trying to run an executable jar file using a makefile. Any help appreciated.

$ ./HelloWorld
-bash: ./HelloWorld: cannot execute binary file

$ file HelloWorld
HelloWorld: Zip archive data, at least v2.0 to extract

$ ls -l
total 32
-rwxr-xr-x  1 myMac  staff  773 Jan 17 06:55 HelloWorld
-rw-r--r--  1 myMac  staff  427 Jan 17 06:55 HelloWorld.class
-rw-r--r--  1 myMac  staff  120 Jan 17 05:52 HelloWorld.java
-rw-r--r--  1 myMac  staff  304 Jan 17 05:59 makefile

These are the 2 files I am using.

HelloWorld.java

class HelloWorld{
 public static void main(String[] args){
 System.out.println("Hello, world!");
 }
}

makefile

HelloWorld: HelloWorld.class
    echo Main-class: HelloWorld > Manifest
    jar cvfm HelloWorld Manifest HelloWorld.class
    rm Manifest
    chmod +x HelloWorld
HelloWorld.class: HelloWorld.java
    javac -Xlint HelloWorld.java
like image 468
sweepez Avatar asked Dec 12 '25 11:12

sweepez


1 Answers

This command:

jar cvfm HelloWorld Manifest HelloWorld.class

creates a jar file (jar files are ZIP archives like file HelloWorld told you). This is not a "normal" binary file that can be run like ./HelloWorld. You need to use Java to run it for you:

java -jar HellorWorld

I also suggests to change the above code to:

jar cvfm HelloWorld.jar Manifest HelloWorld.class

so it will be clear what that file really is.

Btw, since HelloWorld is no binary file you don't need the command chmod +x HelloWorld.

like image 52
Tom Avatar answered Dec 14 '25 01:12

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!