简体   繁体   English

从cli运行时,简化在eclipse中创建的Java程序的类路径条目?

[英]Simplifying entry of classpath for java programs created in eclipse, when run from cli?

I use eclipse as my IDE to save projects onto a samba network drive that I have mapped on my ubuntu home server. 我将eclipse用作IDE,将项目保存到已映射到ubuntu主服务器上的samba网络驱动器上。 I have Eclipse configured to save the jar libraries that I'm using into the /lib folder. 我将Eclipse配置为将我正在使用的jar库保存到/ lib文件夹中。

The problem is that when I try to run my java programs from the command line on the server, I always have to type 问题是,当我尝试从服务器上的命令行运行Java程序时,我总是必须输入

java -cp .:../lib/* javaclassname

This gets a bit tiresome, especially considering tab-completion won't work with the colon that must be included in the classpath. 这有点令人厌烦,尤其是考虑到制表符补全不适用于必须包含在类路径中的冒号。 Is there any way to set things up so I don't have to manually specify the classpath every time? 有没有什么办法可以设置事情,所以我不必每次都手动指定类路径?

The easiest option is to let Eclipse build the JAR for you. 最简单的选择是让Eclipse为您构建JAR。 Right-click on your project, then select Export > Runnable JAR file. 右键单击您的项目,然后选择“导出”>“可运行的JAR文件”。 The dialog will give you a few options. 该对话框将为您提供一些选项。 Select 'Copy required libraries...'. 选择“复制所需的库...”。 The output will be your main application JAR file, plus a lib folder with your dependencies. 输出将是您的主应用程序JAR文件,以及带有您的依赖项的lib文件夹。 In this case, the JAR file manifest will specify the class path, which points to all your dependencies. 在这种情况下,JAR文件清单将指定类路径,该路径指向您的所有依赖项。 Running it at the command line then would be as simple as "java -jar myJar.jar". 在命令行上运行它就像“ java -jar myJar.jar”一样简单。

If you want to automate things, I would start with Ant. 如果您想自动化,我将从Ant开始。 I think it will be less of a learning curve than Maven. 我认为这将比Maven少一些学习曲线。 In this case, check out the jar Ant task . 在这种情况下,请检查jar Ant任务

I ended up solving this by making a shell script called run.sh and putting it in the project's root folder. 我最终通过制作一个名为run.sh的外壳脚本并将其放在项目的根文件夹中来解决了这个问题。

run.sh: run.sh:

#!/bin/bash

LIBS=$(grep '<classpathentry kind="lib" path="' .classpath | sed 's/.*\<classpathentry kind="lib" path="//' | sed 's/\"[^\"]*$//' | tr '\n' ':')

java -cp "bin:$LIBS" $1

You make it executable by running chmod u+x run.sh , then run it with ./run.sh packagename.classname 通过运行chmod u+x run.sh使其可执行,然后使用./run.sh packagename.classname运行它

It reads the .classpath file in the root folder and adds every entry to the colon-separated list on the commandline. 它读取根文件夹中的.classpath文件,并将每个条目添加到命令行中以冒号分隔的列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM