如何在java文件中包含jar文件并在命令提示符下编译 [英] How to include jar files with java file and compile in command prompt

查看:121
本文介绍了如何在java文件中包含jar文件并在命令提示符下编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个jar文件和一个依赖于这些jar文件的.java文件。如何使用命令提示符使用这些jar文件编译.java文件?

I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt?

推荐答案

您可以将jar文件包含在javac命令使用-cp选项。

You can include your jar files in the "javac" command using the "-cp" option.

javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java

而不是-cp你也可以使用-classpath

Instead of "-cp" you could also use "-classpath"

javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java

每次编译时都可以通过正确设置环境变量CLASSPATH来包含jar。环境变量将存储需要用于编译/执行任何java文件的jar和类的路径。每次编译文件时都不必单独包含jar。

You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.

不同的机器有不同的方法将类路径设置为环境变量。
Windows,Linux等的命令是不同的。

Different machines have different methods to set the classpath as an environment variable. The commands for Windows, Linux, etc are different.

您可以在此博客

http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html

这篇关于如何在java文件中包含jar文件并在命令提示符下编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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