简体   繁体   English

如何在 IDEA 中使用类路径而不是模块路径运行 Java 9 应用程序?

[英]How to run Java 9 application with classpath rather than module path in IDEA?

When I run a main class in IDEA, it puts the module and its dependencies on a module path.当我在 IDEA 中运行主类时,它会将模块及其依赖项放在模块路径上。 Is it possible to change it to a classpath?是否可以将其更改为类路径?

If you don't define a module-info, IDEA would set the application and your dependencies on the classpath.如果您不定义模块信息,IDEA 将在类路径上设置应用程序和您的依赖项。 Since you have a module-info it's an explicit module so it has to be on the module path.因为你有一个模块信息,它是一个显式模块,所以它必须在模块路径上。 Normally you would handle your dependecies now as automatic-modules.通常你现在会把你的依赖作为自动模块来处理。

Howsoever, your dependencies at least have a good reason to be on the classpath.但是,您的依赖项至少有充分的理由位于类路径中。 We discussed that here Why Java 9 does not simply turn all JARs on the class path into automatic modules?我们在这里讨论了为什么 Java 9 不简单地将类路径上的所有 JAR 都变成自动模块?

For example, mymodule depends on an automatic-module which however needs a jar that can't become an automatic-module yet.例如,mymodule 依赖于一个自动模块,但是它需要一个还不能成为自动模块的 jar。 On commandline it would like this:在命令行上它会像这样:

java -cp legacy.jar -p "mymodule.jar;automodule.jar;" java -cp legacy.jar -p "mymodule.jar;automodule.jar;" -m mymodule/com.example.mymodule.Application -m mymodule/com.example.mymodule.Application

IMO intellij doesn't currently support that. IMO intellij 目前不支持。 As a workaround to run the entire application on the classpath at least, you could rename the model-info for disabling/not to be a jigsaw-module for a moment.作为至少在类路径上运行整个应用程序的解决方法,您可以重命名模型信息以暂时禁用/不作为拼图模块。


To answer the question, the heuristic IDEA uses to run an application with a module path is currently slightly off (IDEA 2018.01).为了回答这个问题,IDEA 用于运行具有模块路径的应用程序的启发式方法目前略有偏差 (IDEA 2018.01)。 It apparently takes more into consideration than it should.它显然比它应该考虑的更多。

See IDEA-187390 and IDEA-193714 for details.有关详细信息,请参阅IDEA-187390IDEA-193714

So if your current project does not have a module-info file and you still end up with a module path the current case that I know is that you are running the main class from a dependency.因此,如果您当前的项目没有模块信息文件并且您仍然以模块路径结束,我知道的当前情况是您正在从依赖项运行主类。 Just build a small wrapper inside your project and run the main class from there, that brings you back to the classpath.只需在您的项目中构建一个小包装器并从那里运行主类,这将带您回到类路径。

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

相关问题 如何在 IntelliJ IDEA 中的应用程序运行配置文件中将目录添加到类路径? - How to add directory to classpath in an application run profile in IntelliJ IDEA? 在Java中获取当前用户的路径而不是应用程序路径 - Getting the current user's path rather than application path in java 在Linux上提供多个目录作为类路径(带有路径分隔符)时,无法从命令行运行Java应用程序 - Cannot run java application from command line while providing multiple directories as classpath ( with path separator) on linux 如何让类路径上的Java类访问模块路径上属于模块的类 - How to let a java class on classpath access a class belonging to a module on module-path 覆盖 Bash 脚本中的 ClassPath 以运行 Java 应用程序 - Override ClassPath in Bash script to run Java application 无法编译和运行类路径 Java 应用程序 - Not able to compile and run a classpath Java Application 如何在程序运行时使Java程序将特定路径附加到classpath - How to enable java program to append specific path to classpath at program run time Java可以在“绝对”路径下正常运行,而不在指定CLASSPATH下运行 - java runs fine with 'absolute' path, doesn't run with CLASSPATH specified 使用来自 Golang 的类路径和库路径运行 Java 命令 - Run Java command with classpath and library path from Golang Java中的PATH和CLASSPATH变量 - PATH and CLASSPATH variables in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM