从命令行执行Java [英] Execute Java from command line

查看:164
本文介绍了从命令行执行Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌面上有一个名为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>变量。这不是我会在生产或构建/测试盒,但它是非常方便的开发目的。你也想要你的常用罐子。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆