Compiling and running java application using powershell

28,409

Solution 1

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

Solution 2

The variables you speak of do not exist in PowerShell as you name them.

The correct variable names are

$Env:JAVA_HOME: C:\jdk1.6.0;

$Env:PATH: C:\jdk1.6.0\bin;.;

$Env:CLASSPATH: C:\jdk1.6.0\lib;.;

As they all must be defined in the ENV: PSDrive

Share:
28,409
Jquery Developer
Author by

Jquery Developer

Updated on July 09, 2022

Comments

  • Jquery Developer
    Jquery Developer almost 2 years

    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:

    $javac C:\Helloworld.java I get an error "javac is not recognised as a token".
    

    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.

    • Thufir
      Thufir over 4 years
      It's an interesting question, but gradle seems the way to go -- at least for now.
  • Jquery Developer
    Jquery Developer almost 10 years
    I am able to compile and run it through the cmd prompt, I want to do it using powershell scripting only
  • Admin
    Admin almost 10 years
    Please correct me if I am wrong, you have PATH variable set properly and javac is available to execute from any folder and you get the error that javac is not found when you try to exec javac via PowerShell. Am I right?
  • Admin
    Admin almost 10 years
    I have updated my answer, check the update please, there's a link to microsoft technet article where it is explained how to run executables via PowerShell.