【问题标题】:Eclipse issue with Maven build and JDK when generating Qclasses in Querydsl在 Querydsl 中生成 Qclass 时,Maven 构建和 JDK 的 Eclipse 问题
【发布时间】:2014-08-20 08:38:49
【问题描述】:

当我在pom.xml 中添加以下代码以支持 Querydsl

<plugin>
  <groupId>com.mysema.maven</groupId>
  <artifactId>apt-maven-plugin</artifactId>
  <version>1.0.6</version>
  <executions>
    <execution> 
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <outputDirectory>target/generated-sources/java</outputDirectory>
        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
      </configuration>
    </execution>
  </executions>
</plugin>

我在使用 Eclipse 构建时遇到了这个错误。我认为它与类路径和 JDK jars 有关系

You need to run build with JDK or have tools.jar on the classpath.
If this occures during eclipse build make sure you run eclipse under  JDK as well 
(com.mysema.maven:apt-maven-plugin:1.0.6:process:default:generate-sources)

.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>


额外信息:

我的 maven 安装

JAVA_HOME : C:\Program Files\Java\jdk1.7.0_45
PATH : %JAVA_HOME%\bin;

【问题讨论】:

  • 你是如何运行这个项目的 Maven 构建的?在 Eclipse 中还是在命令行中?
  • 我不运行 Maven 构建,我只使用 eclipse,当我将问题中的代码添加到 pom.xml 时出现错误。
  • 如果你去Windows &gt; Preference &gt; Maven &gt; Installations,那里有什么条目?另外,您的.classpath 是什么样的?
  • 当你在 Eclipse 之外运行它时这是否有效,或者这是一个 Eclipse 独有的问题?
  • 我更新了我的问题@DonovanMuller

标签: java eclipse maven querydsl


【解决方案1】:

解决方案 1

关注这个link

“Maven APT 插件有一个已知问题,阻止其使用 直接来自 Eclipse。 Eclipse 用户必须创建 Querydsl 查询 通过在命令中运行命令 mvn generate-sources 手动键入 提示。”

所以我在我的项目中使用控制台cmd 执行命令行mvn generate-sources 并生成了我的Qclasses。

解决方案 2来自@informatik01 评论

我们可以像这样在eclipse.ini 中明确地specified JVM

-vm
C:\Program Files\Java\jdk1.7.0_45\bin\javaw.exe

-vmargs
...

-vm 选项必须出现在-vmargs 选项之前,有关更多信息,请阅读下面的@informatik01 评论。

【讨论】:

  • 我已经进入项目目录,我已经运行了 mvn generate-sources,我已经设置了 Eclipse JDK 版本,但是在 Eclipse 中我仍然无法识别我的 Q 类。当我使用 maven 运行项目时,它可以工作,但是当我查看它时,Eclipse 会在我的代码中出现语法错误。源是在 target/generate-sources 中生成的。是这个问题吗?
  • 您必须将 target/generate-sources/querydsl 添加为源文件夹。默认情况下,目标目录中的任何内容都不会被视为源。
  • 更多指针:- 由于解决方案 2 最初对我不起作用 1. 如果您使用 64 位 JDK,请确保 eclipse 是 64 位。 2. 重启eclipse后可能需要进行eclipse clean build。
【解决方案2】:

你可以在 pom 中试试这个:

<plugin>
  <groupId>com.mysema.maven</groupId>
  <artifactId>apt-maven-plugin</artifactId>
  <version>1.0.6</version>
  <executions>
    <execution> 
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <outputDirectory>target/generated-sources/java</outputDirectory>
        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>com.sun</groupId>
      <artifactId>tools</artifactId>
      <version>1.7</version>
      <scope>system</scope>
      <systemPath>${java.home}/../lib/tools.jar</systemPath>
     </dependency>
  </dependencies>
</plugin>

看看它是否改变了什么。它应该强制 tools.jar 在构建路径中。


编辑。由于这没有帮助,请尝试指定

-vm 
D:/work/Java/jdk1.6.0_13/bin/javaw.exe

在 eclipse.ini 中(单独的行很重要),如 this thread 中所述。

【讨论】:

  • 我试了一下,但仍然出现同样的错误,我认为问题出在You need to run build with JDK 而不是tools.jar 部分
  • @Haricha 他们应该是彼此的替代品。您需要使用 JDK 运行构建或路径中有 tools.jar。但是,如果这对您不起作用,您仍然可以尝试调整 Eclipse 启动选项。 this thread 对此进行了解释
  • 你试过vm选项了吗?
  • 是的,当然,我通过在命令行mvn generate-sources生成源来解决它
  • @eis 您在编辑中的建议是正确的。见my comment
【解决方案3】:

我终于做到了!我尝试了很多选项,例如thisthis,但没有运气。然后我读到了这个comment,它救了我的命,真的,谢谢!我遵循此解决方案及其突然工作!在我的情况下应该被接受。

在我执行 mvn clean install 之后,我将 tools.jar 从 C:\Program Files\Java\jdk1.8.0_151\lib 复制到 C:\Program Files\Java\jre1.8.0_151\lib – @julio mulcue burbano

【讨论】:

    【解决方案4】:

    <groupId>com.mysema.maven</groupId>
            <artifactId>maven-apt-plugin</artifactId>
            <version>1.0.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JAPAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    添加插件,你会解决它

    【讨论】:

      【解决方案5】:

      这个问题发生在我身上,因为如上所述,Eclipse 本身是通过 JRE 而不是 JDK 运行的。

      我通过将 %JAVA_HOME%\bin 添加到我的 PATH 环境变量的前面来解决它。

      我通过阅读了解了 JVM eclipse 使用的是什么:Find out what JVM Eclipse is running on

      【讨论】:

        【解决方案6】:

        如果你通过 shell commond 构建,比如 mvn install。然后在 shell 或 cmd 窗口中运行此命令:echo $CLASSPATH。此命令显示您的类路径。

        如果您使用的是 Eclipse,请打开 Window > Preferences > Java > Installed JREs,确保您安装的 JREs 位于 jdk 根位置。对我来说,它是 C:\Java\jdk1.7.0_51

        希望对你有帮助。

        【讨论】:

        • 它没有用我用额外的信息更新我的问题。
        【解决方案7】:

        尝试在 Eclipse 中更新 JDK,如下所示:

        设置JRE

        看看下面的路径 - 有program files(但在你提到的路径中,你的路径中没有program files,通常java和所有程序都安装在program files中,所以请确保该路径)

         Window->Preferences...->Java->Installed JREs:
        
        JRE type: Standard VM JRE 
        Name: jdk1.6.0_18
        JRE home directory: C:\Program Files (x86)\Java\jdk1.6.0_18
        

        还要确保JAVA_HOME 路径正确设置为JDK\bin(路径中没有任何空格)

        尝试将您的 JDK 复制到其他位置,然后使用该新位置更新您的 JAVA_HOME

        如有任何其他问题,请告诉我。

        【讨论】:

        • 它不起作用,我用额外的信息更新我的问题。
        【解决方案8】:

        不要忘记在 Eclipse 项目设置中检查 执行环境 的设置:Project Build Path -> Libraries -> JRE System Library。。 p>

        如果这是错误的(例如 jre),请将其切换为 jdk

        就我而言,这解决了问题(“您需要使用 JDK 运行构建或在类路径上有 tools.jar。”消失了)。

        然后大约。 Maven 运行日志中的第四行更改(例如)

        ...
        Java home: C:\Program Files\Java\jre1.8.0_66
        ....
        

         ...
         Java home: C:\Program Files\Java\jdk1.8.0_66\jre
         ...
        

        希望这会有所帮助。

        【讨论】:

          【解决方案9】:

          我已经完成了上面建议的所有操作,但无济于事。最后,因为我正在设置一台新计算机,我意识到我没有设置 JAVA_HOME 变量。

          所以我所做的只是将JAVA_HOME 变量添加到我的系统变量中并将其设置为JDK 路径:C:\Program Files\Java\jdk1.8.0_172

          瞧——mvn generate-resources 又开始工作了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-25
            • 2021-01-20
            • 2021-11-06
            • 2016-07-29
            • 1970-01-01
            • 2019-03-30
            • 2020-08-19
            • 1970-01-01
            相关资源
            最近更新 更多