Compiling and running java application using power

2019-03-14 03:36发布

问题:

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.

回答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



回答2:

To answer it in a much simpler way , its the path problem .

You probably not have set env variables that's it.

This is how you should set it:

JAVA_HOME: C:\jdk1.6.0;

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

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

And later if you open a cmd prompt and type java -version , if you are able to see the java installed version then you are good to go.



回答3:

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