-2

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
    ```


 
  • Do you want to use a JAR file or Java class file? In case of Java you have to compile your code first, before you run it, so you'll need to execute `javac MyClass.java` before running it like `java -cp . MyClass` or you'll need some extra script that wil compile and execute the class at once. – Kamil Dec 18 '20 at 10:37
  • 1
    If you have a java source file that is only in one file, then with the later SDK versions you can type ``java Filename.java`` where the filename.java is the source file name. Otherwise do as @Kamil said. I think you almost got it, but you used a colon at the start. – NomadMaker Dec 18 '20 at 11:34

3 Answers3

0

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

AirlineDog
  • 520
  • 8
  • 21
  • It is compiling on its own but after I type `: java filename`, it is showing 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` – Leisure Account Dec 18 '20 at 10:44
  • Make sure that you set up java and the environment variables correctly in order java to be recognised as a command – AirlineDog Dec 18 '20 at 10:54
0

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 .jaror .war java project you can execute it with java -jar hello.jar

Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59
  • While I got to compile, it shows this : `javac : The term 'javac' 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 + javac lol.java + ~~~~~ + CategoryInfo : ObjectNotFound: (javac:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException` – Leisure Account Dec 18 '20 at 10:49
  • Do you set the JAVA_HOME? https://stackoverflow.com/questions/7709041/javac-is-not-recognized-as-an-internal-or-external-command-operable-program-or – Valentyn Hruzytskyi Dec 18 '20 at 11:11
0

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Yaa, as you told it, will compile on its own, but when is run the command as I got to know from many people. After compiling you have to write this `: java filename`, while some told something else after I ran the code it showed me this error. – Leisure Account Dec 18 '20 at 10:53
  • The error is this: `: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` – Leisure Account Dec 18 '20 at 10:53
  • For the above error I had written this `:java filename` in the terminal – Leisure Account Dec 18 '20 at 10:55
  • 1
    FIrst of all: my question describes a mode where you explicitily **don't** need `javac`, as I explained. Second: if you get an error then you should probably write that in your question, the comments are not the right place for discussion. I don't know where you got the `:` from. – Joachim Sauer Dec 18 '20 at 10:55