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

查看:26
本文介绍了如何在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?

推荐答案

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

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

您也可以使用-classpath"代替-cp"

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

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

通过正确设置环境变量CLASSPATH",您可以在每次编译时包含 jars.环境变量将存储需要用于编译/执行任何 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天全站免登陆