如何使用外部库JAR在终端中运行Java程序

时间:2021-03-04 17:31:20

This should be simple but I have never done it before and didn't find any solution.

这应该很简单,但我以前从未做过,也没有找到任何解决方案。

I am currently using Eclipse to code my program, which imports some external JAR library such as google data api library. I can use Eclipse to compile/build/run the program.

我目前正在使用Eclipse来编写我的程序,它导入了一些外部JAR库,例如google data api库。我可以使用Eclipse来编译/构建/运行程序。

But now I want to run it in terminal, so where should I put those JAR files, and how to build and run the program?

但是现在我想在终端中运行它,那么我应该在哪里放置这些JAR文件,以及如何构建和运行程序?

Thanks!

3 个解决方案

#1


67  

You can do :

你可以做 :

1) javac -cp /path/to/jar/file Myprogram.java

1)javac -cp / path / to / jar / file Myprogram.java

2) java -cp .:/path/to/jar/file Myprogram

2)java -cp。:/ path / to / jar / file Myprogram

So, lets suppose your current working directory in terminal is src/Report/

所以,假设您在终端中的当前工作目录是src / Report /

javac -cp src/external/myfile.jar Reporter.java

java -cp .:src/external/myfile.jar Reporter

Take a look here to setup Classpath

看看这里设置Classpath

#2


5  

For compiling the java file having dependency on a jar

用于编译依赖于jar的java文件

javac -cp path_of_the_jar/jarName.jar className.java

javac -cp path_of_the_jar / jarName.jar className.java

For executing the class file

用于执行类文件

java -cp .;path_of_the_jar/jarName.jar className

java -cp。; path_of_the_jar / jarName.jar className

#3


0  

  1. you can set your classpath in the in the environment variabl CLASSPATH. in linux, you can add like CLASSPATH=.:/full/path/to/the/Jars, for example ..........src/external and just run in side ......src/Report/
  2. 您可以在环境变量CLASSPATH中设置类路径。在linux中,你可以添加像CLASSPATH =。:/ full / path /到/ the / Jars,例如.......... src / external,只需在...中运行...... src /报告/

Javac Reporter.java

java Reporter

Similarily, you can set it in windows environment variables. for example, in Win7

类似地,您可以在Windows环境变量中设置它。例如,在Win7中

Right click Start-->Computer then Properties-->Advanced System Setting --> Advanced -->Environment Variables in the user variables, click classPath, and Edit and add the full path of jars at the end. voila

右键单击用户变量中的开始 - >计算机,然后选择属性 - >高级系统设置 - >高级 - >环境变量,单击classPath,然后编辑并在末尾添加完整的罐子路径。瞧

#1


67  

You can do :

你可以做 :

1) javac -cp /path/to/jar/file Myprogram.java

1)javac -cp / path / to / jar / file Myprogram.java

2) java -cp .:/path/to/jar/file Myprogram

2)java -cp。:/ path / to / jar / file Myprogram

So, lets suppose your current working directory in terminal is src/Report/

所以,假设您在终端中的当前工作目录是src / Report /

javac -cp src/external/myfile.jar Reporter.java

java -cp .:src/external/myfile.jar Reporter

Take a look here to setup Classpath

看看这里设置Classpath

#2


5  

For compiling the java file having dependency on a jar

用于编译依赖于jar的java文件

javac -cp path_of_the_jar/jarName.jar className.java

javac -cp path_of_the_jar / jarName.jar className.java

For executing the class file

用于执行类文件

java -cp .;path_of_the_jar/jarName.jar className

java -cp。; path_of_the_jar / jarName.jar className

#3


0  

  1. you can set your classpath in the in the environment variabl CLASSPATH. in linux, you can add like CLASSPATH=.:/full/path/to/the/Jars, for example ..........src/external and just run in side ......src/Report/
  2. 您可以在环境变量CLASSPATH中设置类路径。在linux中,你可以添加像CLASSPATH =。:/ full / path /到/ the / Jars,例如.......... src / external,只需在...中运行...... src /报告/

Javac Reporter.java

java Reporter

Similarily, you can set it in windows environment variables. for example, in Win7

类似地,您可以在Windows环境变量中设置它。例如,在Win7中

Right click Start-->Computer then Properties-->Advanced System Setting --> Advanced -->Environment Variables in the user variables, click classPath, and Edit and add the full path of jars at the end. voila

右键单击用户变量中的开始 - >计算机,然后选择属性 - >高级系统设置 - >高级 - >环境变量,单击classPath,然后编辑并在末尾添加完整的罐子路径。瞧