eclipse 如何在eclipse中通过相对路径导入外部库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4980097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import external library by relative path in eclipse?
提问by ablimit
I imported external libraries using absolute path. But I have two work environments, switching between Linux and Windows. Projects are downloaded from SVN. So I was wondering whether I can import these libraries by relative path.
我使用绝对路径导入了外部库。但是我有两个工作环境,在 Linux 和 Windows 之间切换。项目从SVN下载。所以我想知道我是否可以通过相对路径导入这些库。
采纳答案by FrVaBe
You should declare a variable (Java Build Path -> Add Variable... -> Configure Variable ... -> New) to set the changing path on each system (e.g. FOO_BAR_HOME).
您应该声明一个变量(Java Build Path -> Add Variable... -> Configure Variable ... -> New)来设置每个系统上的更改路径(例如 FOO_BAR_HOME)。
Than you can add the Variable to the Libraries and edit it to point to your library, like
然后您可以将变量添加到库中并编辑它以指向您的库,例如
%FOO_BAR_HOME%/lib/foobar.jar
Take a look at the existing variables for usage.
查看现有变量以供使用。
Alternative you can place the library inside the project (e.g. subfolder 'lib'). If you add the library from this location ('Add Jars...' NOT 'Add External Jars...') it will be added by relative path.
或者,您可以将库放在项目中(例如子文件夹“lib”)。如果您从此位置添加库(“添加罐子...”而不是“添加外部罐子...”),它将通过相对路径添加。
回答by aljo
Hey I was having this issue as well, but I can offer an alternate solution for those who want to keep the required library locallybut outsidethe directory of the Java Project.
嘿,我也有这个问题,但我可以为那些想要在本地但在 Java 项目目录之外保留所需库的人提供替代解决方案。
First, add the library you want using the usual "Add External Library/Jar" method.
首先,使用通常的“添加外部库/Jar”方法添加您想要的库。
If you go into the home directory of your Java Project, you should see a .classpath
file. Edit this with any text editor you wish, and look for the line that references the library you added. Then, say your library is called lib.jar
and you have it one level outside of your project.
如果您进入 Java 项目的主目录,您应该会看到一个.classpath
文件。使用您希望的任何文本编辑器编辑它,并查找引用您添加的库的行。然后,假设您的库被调用,lib.jar
并且您在项目之外拥有它一层。
Lets say the line says:
让我们说这条线说:
<classpathentry kind="lib" path="C:/Users/Public/workspace/lib.jar"/>
<classpathentry kind="lib" path="C:/Users/Public/workspace/lib.jar"/>
Rather than moving lib.jar
to your Project's directory, you can just change this line to say:
而不是移动lib.jar
到您的项目的目录,您可以更改这一行说:
<classpathentry kind="lib" path="./../lib.jar"/>
<classpathentry kind="lib" path="./../lib.jar"/>
and your Java Build path will be updated to look for it in the directory one level above the current project.
并且您的 Java Build 路径将被更新以在当前项目上一级的目录中查找它。
Also, to my knowledge if you're running a Mac or any Unix based OS, then the .classpath file will be hidden. In order to access it, you can try opening it from Terminal, by navigating to the directory that your Java Project uses (the actual project folder itself, not the workspace), then either vi or vim ".classpath"
. Keep in mind that using ls or even ls -a might not work, but trust that it will be there.
另外,据我所知,如果您运行的是 Mac 或任何基于 Unix 的操作系统,那么 .classpath 文件将被隐藏。为了访问它,您可以尝试从终端打开它,方法是导航到您的 Java 项目使用的目录(实际项目文件夹本身,而不是工作区),然后是 vi 或 vim ".classpath"
。请记住,使用 ls 甚至 ls -a 可能不起作用,但请相信它会在那里。
Once you've changed it to the specified location, save the file and when you go back into eclipse, the reference should still be working. Keep in mind that the "./"
at the beginning is necessary. Just having "../"
will not work, or at least it didn't for me.
将其更改为指定位置后,保存文件,当您返回 eclipse 时,引用应该仍然有效。请记住,"./"
开头是必要的。仅仅拥有是"../"
行不通的,或者至少对我没有用。
回答by glaucomardano
I did it very simple. I had a lib with an absolute path in my classpath:
我做的很简单。我的类路径中有一个带有绝对路径的库:
/home/glauco/workspace/ltClubs/lib/swingx-core-1.6.2.jar
So i just removed the absolute path from it and it works. Now it's relative xD:
所以我只是从中删除了绝对路径并且它可以工作。现在是相对的 xD:
lib/swingx-core-1.6.2.jar
回答by luispablo
This should be a comment on the previous answer, but the strange reputation system from this site forces me to post a new answer instead... (no comments)
这应该是对上一个答案的评论,但是这个网站奇怪的声誉系统迫使我发布一个新答案......(没有评论)
You can use a relative path, but you're missing the './' in it.
您可以使用相对路径,但其中缺少“./”。
Instead of
代替
lib/swingx-core-1.6.2.jar
you should use
你应该使用
./lib/swingx-core-1.6.2.jar
回答by gunaprsd
Add the folder containing the dependencies into the JAVA project. Select and right-click the dependencies, "Add to Build Path" pops up as a context menu. This adds the dependencies using relative path instead of the absolute path.
将包含依赖项的文件夹添加到 JAVA 项目中。选择并右键单击依赖项,“添加到构建路径”作为上下文菜单弹出。这使用相对路径而不是绝对路径添加依赖项。