Compiling and running java application using powershell

Question:

I am trying to compile and a sample Helloworld.java file.
I have my jdk installed in C:\Program Files\jdk1.7\bin.

And I have my Helloworld.java in C:\Helloworld.java

I am actually a novice in both powershell and java.
I got some examples from web regarding this but many of them advice to run it like this:

java.exe -classpath $Env:CLASSPATH C:\Helloworld.java

But when I give this in powershell I get an error called ‘CLASSPATH’ is not defined even after adding it in env variables.

And when I try to compile the code with the following syntax:

So, I am literally lost in this topic . Any step by step procedure to run java programs using powershell for dummies like me will be greatly appreciated.

Answer:

Setup environment variables in your system.

set JAVA_HOME to C:\Program Files\jdk1.7

add to PATH variable the string %JAVA_HOME%\bin

open new cmd session.

navigate your java source folder.

use javac to compile your java files.

UPDATE:

also if you are experiencing difficulities upon launching an executable via PowerShell check this Microsoft TechNet article

Source:

Compiling and running java application using powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply