在linux中编译和运行java程序,其中包含.java文件的路径和外部jar的路径 [英] Compile and Run java program in linux with path to the .java file and path to external jars

查看:148
本文介绍了在linux中编译和运行java程序,其中包含.java文件的路径和外部jar的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在stackoverflow中解决了一个问题。但我最终遇到了其他问题,但我会尽量明确:

Yesterday I solve a problem with an answer here in stackoverflow. But I ended up with other problem, but I will try to be clear:

我在/ home / demo / Desktop / xlsToCsv /目录中有一个项目文件夹里面的它是java文件xlsToCsv.java和另外一个带有我需要的外部jar的目录,在/ home / demo / Desktop / xlsToCsv / jars。

I have a project folder in /home/demo/Desktop/xlsToCsv/ directory where inside of it is the java file "xlsToCsv.java" and another directory with the external jars that I need, in, /home/demo/Desktop/xlsToCsv/jars.

现在我需要编译并运行我的程序。昨天我问了这个,我运行了一个假设我已经在/ home / demo / Desktop / xlsToCsv /中的命令,命令是:

Now I need to compile and run my program. Yesterday I asked this and I ran a command that assumed that I was already inside of /home/demo/Desktop/xlsToCsv/, and the command was:


javac -cp。:。/ jars / *xlsToCsv.java

javac -cp ".:./jars/*" xlsToCsv.java

java -cp。:。/。jars / *xlsToCsv

java -cp ".:./jars/*" xlsToCsv

问题解决了,我能够运行我的程序,没有任何问题。但是,我的程序是从根目录运行,即打开linux终端时的目录,而不需要发出cd命令。

The problem was solved and I was able to run my program withou any problems. But, my program as to run from the root directory, ie, the directory where it is when I open the linux terminal without the need to make a "cd" command.

所以,当我打开终端时,.java文件的路径是:

So, when I open the terminal the path to the .java file is:


/ home / demo / Desktop / xlsToCsv /

/home/demo/Desktop/xlsToCsv/

jars文件夹的路径是:

And the path to jars folder is:


/ home / demo / Desktop / xlsToCsv / jars / *

/home/demo/Desktop/xlsToCsv/jars/*

有人可以向我解释我要做什么,以及原因所以?因为更多运行程序,我想知道原因并理解java中的类路径机制。

Can someone explain to me what I have to do, and the reason so? Because more that run the program, I want to know the reasons and understand the classpath mechanism in java.

谢谢

推荐答案

避免使用相对类路径。和./jars/ 的instread使用绝对路径/ home / demo / Desktop / xlsToCsv / jars /

Avoid using relative classpath. and instread of "./jars/" use the absolute path "/home/demo/Desktop/xlsToCsv/jars/"

编辑:

javac -cp "/home/demo/Desktop/xlsToCsv/jars/*" /home/demo/Desktop/xlsToCsv/xlsToCsv.java

java -cp "/home/demo/Desktop/xlsToCsv/:/home/demo/Desktop/xlsToCsv/jars/*" xlsToCsv

这篇关于在linux中编译和运行java程序,其中包含.java文件的路径和外部jar的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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