Java 如何在 IntelliJ IDEA 中的应用程序运行配置文件中将目录添加到类路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/854264/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:13:41  来源:igfitidea点击:

How to add directory to classpath in an application run profile in IntelliJ IDEA?

javaintellij-ideaclasspath

提问by sal

I'm trying to add a directory to the classpath of an application run profile

我正在尝试将目录添加到应用程序运行配置文件的类路径中

If I override by using -cp x:target/classes in the VM settings, I get the following error:

如果我在 VM 设置中使用 -cp x:target/classes 进行覆盖,则会出现以下错误:

java.lang.NoClassDefFoundError: com/intellij/rt/execution/application/AppMain

Any idea on how to add a directory to the classpath for my project?

关于如何将目录添加到我的项目的类路径的任何想法?

采纳答案by stivlo

In Intellij 13, it looks it's slightly different again. Here are the instructions for Intellij 13:

在 Intellij 13 中,它看起来又略有不同。以下是 Intellij 13 的说明:

  1. click on the Project view or unhide it by clicking on the "1: Project" button on the left border of the window or by pressing Alt + 1
  2. find your project or sub-module and click on it to highlight it, then press F4, or right click and choose "Open Module Settings" (on IntelliJ 14 it became F12)
  3. click on the dependencies tab
  4. Click the "+" button on the right and select "Jars or directories..."
  5. Find your path and click OK
  6. In the dialog with "Choose Categories of Selected File", choose Classes(even if it's properties), press OK and OK again
  7. You can now run your application and it will have the selected path in the class path
  1. 单击项目视图或通过单击窗口左边框上的“1:项目”按钮或按 Alt + 1 取消隐藏它
  2. 找到您的项目或子模块并单击它以突出显示它,然后按 F4,或右键单击并选择“打开模块设置”(在 IntelliJ 14 上它变成了 F12)
  3. 单击依赖项选项卡
  4. 单击右侧的“+”按钮并选择“罐子或目录...”
  5. 找到您的路径并单击确定
  6. 在“选择所选文件的类别”对话框中,选择Classes(即使是属性),再次按确定和确定
  7. 您现在可以运行您的应用程序,它将在类路径中具有选定的路径

回答by Mike Pone

I am using Idea 8. in your module dependancies tab (in the project structure dialog). Add a "Module Library". There you can select a Jar Directory to add. Then make sure the run profile is using the Classpath and JDK of the correct module when it runs (this is in the run config dialog.

我在您的模块依赖项选项卡(在项目结构对话框中)中使用 Idea 8.。添加“模块库”。您可以在那里选择要添加的 Jar 目录。然后确保运行配置文件在运行时使用了正确模块的类路径和 JDK(这在运行配置对话框中。

回答by uaaquarius

You can try -Xbootclasspath/a:pathoption of java application launcher. By description it specifies "a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path."

您可以尝试java 应用程序启动器的-Xbootclasspath/a:path选项。通过描述,它指定“要附加到默认引导程序类路径的目录、JAR 档案和 ZIP 档案的冒号分隔路径”。

回答by samkass

It appears that IntelliJ 11 has changed the method, and the checked answer no longer works for me. In case anyone else arrives here via a search engine, here's how I solved it in IntelliJ 11:

看来 IntelliJ 11 已经改变了方法,并且检查的答案不再适合我。如果其他人通过搜索引擎到达这里,这是我在 IntelliJ 11 中解决它的方法:

  1. Go to the Project Structure, click on Modules, and click on your Module
  2. Choose the "Dependencies" tab
  3. Click the "+" button on the right-hand side and select "Jars or directories..."
  4. Add the directory(ies) you want (note you can multi-select) and click OK
  5. In the dialog that comes up, select "classes" and NOT "jar directory"
  6. Make sure you're using that Module in your run target
  1. 转到项目结构,单击模块,然后单击您的模块
  2. 选择“依赖项”选项卡
  3. 单击右侧的“+”按钮并选择“罐子或目录...”
  4. 添加您想要的目录(注意您可以多选)并单击确定
  5. 在出现的对话框中,选择“类”而不是“jar 目录”
  6. 确保您在运行目标中使用该模块

Note that step 5 seems to be the key difference. If you select "jar directory" it will look exactly the same in the IDE but won't include the path at runtime. There appears to be no way to determine whether you've previously selected "classes" or "jar directory" after the fact.

请注意,第 5 步似乎是关键区别。如果您选择“jar 目录”,它将在 IDE 中看起来完全相同,但不会在运行时包含路径。事后似乎无法确定您之前是否选择了“类”或“jar 目录”。

回答by Oleksandr Bondarenko

Suppose you need only x:target/classesin your classpath. Then you just add this folder to your classpath and %IDEA%\lib\idea_rt.jar. Now it will work. That's it.

假设您的路径中只需要x:target/classes。然后您只需将此文件夹添加到您的类路径和 %IDEA%\lib\idea_rt.jar。现在它将起作用。就是这样。

回答by VNN456

You need not specify the classes folder. Intellij should be able to load it. You will get this error if "Project Compiler output" is blank.

您无需指定类文件夹。Intellij 应该能够加载它。如果“项目编译器输出”为空白,您将收到此错误。

Just make sure that below value is set: Project Settings -> Project -> Project Compiler outputto your projectDir/outfolder

只需确保设置了以下值: 项目设置 -> 项目 -> 项目编译器输出到您的projectDir/out文件夹

回答by ztheitroads

Set "VM options" like: "-cp $Classpath$;your_classpath"

设置“VM 选项”,如:“-cp $Classpath$;your_classpath”

VM options

虚拟机选项