从命令行执行Java [英] Execute Java from command line
问题描述
我在桌面上有一个名为Stuff的文件夹,在该文件夹中有以下内容:
- Hello.java
-
- mail.jar
Hello.java从mail.jar导入,因此我需要告诉Hello.java来寻找mail.jar。
从Windows命令行和unix命令行,我如何编译并运行这个?
编译:
javac -cp 。; mail.jar Hello.java
其中;
用于Windows;
并运行:
java -cp;; mail.jar您好
再次,对于Windows,使用;
,对于* nix使用:
。
-cp
告诉 javac
和 java
classpath 以使用,并且由于您的文件位于执行命令的本地目录中,因此您可以使用。
作为Hello部分, jar内的路径的jar名称。维基百科有关于类路径的体面文章。
提醒你,如果你打算定期这样做,你可能需要设置 CLASSPATH
环境变量,而不是一直使用 -cp
标志。 java
和 javac
使用 CLASSPATH
p>
对于我自己的开发机器,我实际上在我的 CLASSPATH $ c $中包括
。
c>变量。这不是我会在生产或构建/测试盒,但它是非常方便的开发目的。你也想要你的常用罐子。
I have a folder on my desktop titled "Stuff" and in that folder I have the following:
- Hello.java
- mail.jar
And Hello.java imports from mail.jar, so I need to tell Hello.java to look for mail.jar.
From a Windows command line and from a unix command line, how can I compile this and run this?
Compile:
javac -cp .;mail.jar Hello.java
where ;
is for Windows; use :
for *nix.
and run:
java -cp .;mail.jar Hello
where again, use ;
for Windows and :
for *nix.
-cp
tells both javac
and java
what classpath to use, and as your files are in the local directory where you're executing the command, you can use .
for the Hello part and the name of the jar for the paths inside the jar. Wikipedia has a decent article on classpaths.
Mind you, if you're going to be doing this on a regular basis, you may want to set your CLASSPATH
environment variable rather than constantly using the -cp
flag. Both java
and javac
use the CLASSPATH
variable.
For my own development machine, I actually include .
in my CLASSPATH
variable, for convenience. It's not something I would do on a production or build/test box, but it's very handy for development purposes. You'd want to have your usual jars in it as well.
这篇关于从命令行执行Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!