How to run a Java 9 main class in Eclipse without editing a launch-configuration?

旧城冷巷雨未停 提交于 2019-12-12 19:30:33

问题


I created a simple Hello World main class and a module-info.java file within a Maven project in Eclipse Oxygen.1a Release (4.7.1a), running on Java 9.0.1.

When I try to start the main class with right-click - Run As Java Application I get the error:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module com.github.gv2011.quarry.modules.moda not found

I can successfully run it from the command line from the target/classes directory:

java -p . -m com.github.gv2011.quarry.modules.moda/com.github.gv2011.quarry.modules.moda.Hello
Hello

If I manually edit the created launch configuration and add the VM arguments

-p target/classes
-m com.github.gv2011.quarry.modules.moda/com.github.gv2011.quarry.modules.moda.Hello

it works, too.

Is there a more comfortable way to start main classes from Eclipse without that manual launch-configuration editing?

Main class:

package com.github.gv2011.quarry.modules.moda;
public class Hello {
  public static void main(final String[] args) {
    System.out.println("Hello");
  }
}

module-java:

module com.github.gv2011.quarry.modules.moda {
  exports com.github.gv2011.quarry.modules.moda;
}

Related question: Eclipse - module not found when adding module-info.java

The launch configuration created by Eclipse (not working) is this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
        <listEntry value="/quarry-modules-a/src/main/java/com/github/gv2011/quarry/modules/moda/Hello.java"/>
    </listAttribute>
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
        <listEntry value="1"/>
    </listAttribute>
    <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
    <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.github.gv2011.quarry.modules.moda.Hello"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="quarry-modules-a"/>
    <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
</launchConfiguration>

The working one is that:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
        <listEntry value="/quarry-modules-a/src/main/java/com/github/gv2011/quarry/modules/moda/Hello.java"/>
    </listAttribute>
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
        <listEntry value="1"/>
    </listAttribute>
    <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
    <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.github.gv2011.quarry.modules.moda.Hello"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="quarry-modules-a"/>
    <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
    <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-p target/classes&#13;&#10;-m com.github.gv2011.quarry.modules.moda/com.github.gv2011.quarry.modules.moda.Hello"/>
</launchConfiguration>

回答1:


This will be fixed in Eclipse Photon (I've checked with 4.8M6).

If you don't want to wait until June, you can download the current milestone build from https://www.eclipse.org/downloads/index-developer.php

Setting the modulepath was previously simply not implemented in m2e for Oxygen (it was implemented for Photon in https://bugs.eclipse.org/bugs/show_bug.cgi?id=529398.)




回答2:


As pointed in the comments, there is an Eclipse bug which unfortunately got little attention (this looks like a blocker for me which should have been fixed immediately).

As for now, I think this is the simplest workaround:

  • Go to your launch configuration and find VM arguments.
  • Enter -p ${project_classpath:module_name} (substitute module_name with a real module name).


来源:https://stackoverflow.com/questions/47728515/how-to-run-a-java-9-main-class-in-eclipse-without-editing-a-launch-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!