3

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
4
  • This is all described in the Princeton class material which also includes the answer. Commented Jan 17, 2015 at 15:33
  • @BrianTompsett Thanks, just checked the link you sent. However they use $java HelloWorld to run the HelloWorld.class. I can do that just fine. I'm trying to create an executable so as to only have to type $./HelloWorld to run program. Commented Jan 17, 2015 at 15:38
  • As Tom has said below; everything described in the Princeton class is correct. That is how the world does it. You really need to check with your tutor why your school's computer does it differently. Perhaps they use a different Java compiler that generates executable code, but then this is not the standard compiler the rest of the world uses. It is hard for us to answer when your school is apparently doing it different; either that or you have misunderstood something. The only way to know is to ask at School. What Tom and I have said is correct; which was why gave you the link. Commented Jan 17, 2015 at 15:45
  • @BrianTompsett Thanks for trying to help. We already compile how "the world" does it. We use $ javac to compile and $ java to run. My question was about creating an executable jar file using a makefile. My question has been answered. Commented Jan 19, 2015 at 23:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Tom, are there any changes I can make so as to run it as a binary file. When I ssh with terminal to the school's UNIX environment, the file runs fine just using ./HelloWorld. Those were the makefile instructions given by the professor, I was hoping not to have to use $ java -jar HelloWorld to run the file on my local machine and instead just use ./HelloWorld
Have you checked that their HelloWorld is? It might be a simple shell script that just calls the real Java program.
wait, say that again. check what? sorry :(
Their HelloWorld is the same I provided above.
thanks for your help. yeah, the school should be running some script to run the jar file with just the filename. I'll check on that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.