使用powershell编译和运行java应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24464295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling and running java application using powershell
提问by Jquery Developer
I am trying to compile and a sample Helloworld.java file. I have my jdk installed in C:\Program Files\jdk1.7\bin.
我正在尝试编译一个示例 Helloworld.java 文件。我的 jdk 安装在 C:\Program Files\jdk1.7\bin 中。
And I have my Helloworld.java in C:\Helloworld.java
我在 C:\Helloworld.java 中有我的 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:
我实际上是 powershell 和 java 的新手。我从网上得到了一些关于此的示例,但其中许多建议像这样运行它:
java.exe -classpath $Env:CLASSPATH C:\Helloworld.java
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.
但是当我在 powershell 中给出这个时,即使将它添加到 env 变量中,我也会收到一个名为“CLASSPATH”的错误。
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.
所以,我真的迷失在这个话题中。对于像我这样的傻瓜,使用 powershell 运行 java 程序的任何分步过程都将不胜感激。
回答by Jquery Developer
Setup environment variables in your system.
在系统中设置环境变量。
set JAVA_HOME
to C:\Program Files\jdk1.7
设置JAVA_HOME
为C:\Program Files\jdk1.7
add to PATH
variable the string %JAVA_HOME%\bin
添加到PATH
变量字符串%JAVA_HOME%\bin
open new cmd
session.
打开新cmd
会话。
navigate your java source folder.
导航您的 java 源文件夹。
use javac
to compile your java files.
使用javac
编译您的Java文件。
UPDATE:
更新:
also if you are experiencing difficulities upon launching an executable via PowerShell check this Microsoft TechNet article
此外,如果您在通过 PowerShell 启动可执行文件时遇到困难,请查看这篇Microsoft TechNet 文章
回答by user1400915
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.
稍后,如果您打开 cmd 提示符并键入 java -version ,如果您能够看到已安装的 java 版本,那么您就可以开始了。
回答by Marty Wiedmeyer
The variables you speak of do not exist in PowerShell as you name them.
你所说的变量在你命名的 PowerShell 中不存在。
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
因为它们都必须在 ENV 中定义:PSDrive