简体   繁体   中英

How do I run a java program in the terminal using short form?

I started using Java in VS Code, so I just wanted to know the short form to run the Java program. For ex:

If you want to run a Python program in the terminal, we write py filename.py . So, in the same way, I wanted to know for Java if it is there

My code has already been compiled, but after I write :java filename as many people told, I get this error:

:java : The term ':java' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ :java lol
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (:java:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    ```


 

You first need to compile one time the programm by: javac fileName.java

And then every time you want to run it you do: java fileName

Type javac MyFirstJavaProgram.java and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set).

Now, type java MyFirstJavaProgram to run your program.

Also if you have .jar or .war java project you can execute it with java -jar hello.jar

Starting with Java 11 there's a single-file mode that makes running simple programs easy.

Basically if your whole program fits into a single .java file, then you can simply use this to execute it:

java YourFile.java

Note that you don't need to call the compiler first, as in this mode the java binary will actually compile your code for you.

Note that this is strictly restricted to code that fits into a single file. As soon as you split your code over multiple files (which is usually as soon as you have more than one class) you'll need to go the traditional way which implies a separate compile step, followed by executing the compiled classes.

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