如何为 javac 设置 PATH 变量,以便我可以手动编译我的 .java 作品?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2079635/
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
How can I set the PATH variable for javac so I can manually compile my .java works?
提问by Sergio Tapia
Here's the address on my drive:
这是我驱动器上的地址:
C:\Program Files\Java\jdk1.6.0_18\bin
C:\Program Files\Java\jdk1.6.0_18\bin
How would I go about setting the path variable so I can go in command window (windowskey+r "cmd") and be able to type things like:
我将如何设置路径变量,以便我可以进入命令窗口(windowskey+r“cmd”)并能够输入以下内容:
javac TestApp.java
I'm using Windows 7 Professional.
我正在使用 Windows 7 专业版。
采纳答案by Carl Smotricz
Typing the SET PATH
command into the command shell every time you fire it up could get old for you pretty fast. Three alternatives:
SET PATH
每次启动命令时将命令输入命令外壳可能会很快变老。三种选择:
- Run javac from a batch (
.CMD
) file. Then you can just put theSET PATH
into that file before yourjavac
execution. Or you could do without theSET PATH
if you simply code the explicit path tojavac.exe
- Set your enhanced, improved
PATH
in the "environment variables" configuration of your system. - In the long run you'll want to automate your Java compiling with Ant. But that will require yet another extension to
PATH
first, which brings us back to (1) and (2).
- 从批处理 (
.CMD
) 文件运行 javac 。然后您可以SET PATH
在javac
执行之前将其放入该文件中。或者,SET PATH
如果您只是将显式路径编码为javac.exe
PATH
在您的系统的“环境变量”配置中设置您的增强、改进。- 从长远来看,您会希望使用Ant自动进行 Java 编译。但这将需要对
PATH
first进行另一个扩展,这使我们回到 (1) 和 (2)。
回答by x4u
That would be:
那将是:
set "PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
You can also append ;C:\Program Files\Java\jdk1.6.0_18\bin
to the PATH in the user environment dialog. That would allow you to use javac and other java tools directly form any cmd shell without setting the path first. The user environment dialog used to be somewhere in the system properties in XP, I have no idea where it is in Windows 7.
您还可以附加;C:\Program Files\Java\jdk1.6.0_18\bin
到用户环境对话框中的 PATH。这将允许您直接使用 javac 和其他 java 工具形成任何 cmd shell,而无需先设置路径。用户环境对话框曾经位于 XP 系统属性中的某个位置,我不知道它在 Windows 7 中的位置。
回答by RAi
- Type
cmd
in program start - Copy and Paste following on dos prompt
- 类型
cmd
在程序启动 - 在 dos 提示下复制并粘贴以下内容
set PATH="%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
设置 PATH="%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
回答by Galen Nare
You don't need to do any complex command-line stuff or edit any system code. You simply have to open Computer, showing all of your disks and open properties. From there, go to Advanced System Settings and click Environment Variables. Scroll down in the lower list box and edit Path. Do not erase anything already there. Put a ; after it and then type in your path. To test, open command prompt and do "javac", it should list around 20 programs. You would be finished at that point.
您不需要执行任何复杂的命令行操作或编辑任何系统代码。您只需打开计算机,显示所有磁盘并打开属性。从那里,转到高级系统设置,然后单击环境变量。在下方的列表框中向下滚动并编辑路径。不要擦除任何已经存在的东西。放一个 ; 在它之后,然后输入您的路径。要测试,打开命令提示符并执行“javac”,它应该列出大约 20 个程序。到时候你就完蛋了。
By the way, the command to compile is javac -g
not just javac
.
顺便说一下,编译的命令javac -g
不仅仅是javac
.
Happy coding!
快乐编码!
回答by Pratap Singh
Follow the steps given here
按照此处给出的步骤操作
after setting variable, just navigate to your java file directory in your cmd and type javac "xyx.java"
设置变量后,只需导航到 cmd 中的 java 文件目录并键入 javac“xyx.java”
or if you don't navigate to the directory, then simply specify the full path of java file
或者如果您不导航到该目录,则只需指定 java 文件的完整路径
javac "/xyz.java"
javac "/xyz.java"
回答by NishantM
First thing I wann ans to this imp question: "Why we require PATH To be set?"
我想回答这个小鬼问题的第一件事:“为什么我们需要设置 PATH?”
Answer :You need to set PATH to compile Java source code, create JAVA CLASS FILES and allow Operating System to load classes at runtime.
答:您需要设置 PATH 来编译 Java 源代码、创建 JAVA 类文件并允许操作系统在运行时加载类。
Now you will understand why after setting "javac" you can manually compile by just saying "Class_name.java"
现在你会明白为什么在设置“ javac”后你可以通过只说“Class_name.java”来手动编译
Modifythe PATHof Windows Environmental Variable by appending the location till bin directory where all exe file(for eg. java,javac) are present.
通过将位置附加到所有 exe 文件(例如 java、javac)所在的 bin 目录来修改Windows 环境变量的PATH。
Example :;C:\Program Files\Java\jre7\bin.
示例:;C:\Program Files\Java\jre7\bin。
回答by Capcha
Step 1: Set the PATH variable JAVA_HOME to the path of the JDK present on the system. Step 2: in the Path variable add the path of the C:\Program Files\Java\jdk(version)\bin
步骤 1:将 PATH 变量 JAVA_HOME 设置为系统上存在的 JDK 的路径。第二步:在Path变量中添加C:\Program Files\Java\jdk(version)\bin的路径
This should solve the problem. Happy coding!!
这应该可以解决问题。快乐编码!!
回答by Pushp Singh
only this will work:
只有这样才能工作:
path=%set path%;C:\Program Files\Java\jdk1.7.0_04\bin
path=%set path%;C:\Program Files\Java\jdk1.7.0_04\bin
回答by prettyUgly
Trying this out on Windows 10, none of the command-line instructions worked.
在 Windows 10 上尝试此操作,命令行指令均无效。
Right clicking on "Computer" then open Properties etc. as the post by Galen Nare above already explains, leads you to a window where you need to click on "new" and then paste the path (as said: without deleting anything else). Afterwards you can check by typing java -version in the command-line window, which should display your current java version, if everything worked out right.
右键单击“计算机”,然后打开“属性”等,正如上面 Galen Nare 的帖子已经解释的那样,引导您进入一个窗口,您需要在其中单击“新建”然后粘贴路径(如上所述:不删除任何其他内容)。之后,您可以通过在命令行窗口中键入 java -version 进行检查,如果一切正常,它应该会显示您当前的 java 版本。