简体   繁体   中英

can't run .jar file /java.jar: line 1: public: command not found

I have Ubuntu 16.04.

and downloaded a JDK with a tar.gz file extension and followed This wikihow to install it.

When I try to run a .jar game (like Minecraft) It works successfully, and I have netbeans downloaded that is connected to the same JDK and compiled some programs that i can run in terminal, But When I type :

./Hello_world.jar

Which is :

package main;

public class project {

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

I get this output :

./Hello_world.jar: line 1: $'PK\003\004': command not found
./Hello_world.jar: line 2: $'\b.\020oK': command not found
./Hello_world.jar: line 3: syntax error near unexpected token `)'
./Hello_world.jar: line 3:-oK�}����META-INF/MANIFEST.MFM�1
                                                        �0��@��uHh          Q���X� ��N1�Ҧ$)��7�(�p�ww
�A����|��}�1���ή�n��p<�Рŗ��:CpN~�s�ν�˚�3��%
                                                 ��)���goPK`

Simple: JAR files aren't executables. You can only invoke binaries/scripts by telling your shell to ./command .

They are archives that contain compiled Java classes.

Thus you use them like:

java -jar somejar.jar

This starts a java virtual machine, and tells it to open the given JAR file. The JVM will then figure the "main" class to run from the meta information that can be backed into the JAR file - to then "run" that main class.

( assuming that the corresponding JAR file has been built in a why that allows running it like this. see here for details on how you enable this "easy way" of running a JAR file )

And just in case: with some scripting magic, you actually can turn a JAR file into a "binary", see here for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM