Eclipse在使用Maven构建和生成Querydsl中的Q类时出现JDK问题。

26

当我在我的 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>


额外信息:

enter image description here

我的Maven安装

enter image description here

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


1
我不运行Maven构建,我只使用Eclipse,当我将代码添加到我的问题中的pom.xml时,出现了错误。 - Hayi
如果您前往 Windows > Preference > Maven > Installations,那里有哪些条目?另外,您的 .classpath 是什么样子的? - Donovan Muller
1
我更新了我的问题 @DonovanMuller - Hayi
@eis 我只使用Eclipse工作。 - Hayi
@Haricha 我已经相应地更新了你的问题,因为这似乎是一个Eclipse问题,而不是Spring或Maven的问题。 - eis
显示剩余2条评论
9个回答

47

解决方案1

按照链接所述:

"Maven APT插件存在已知问题,会阻止其在Eclipse中直接使用。Eclipse用户必须通过在命令提示符中运行mvn generate-sources命令手动创建Querydsl查询类型。"

因此,在我的项目文件夹中使用控制台cmd执行命令行mvn generate-sources,然后我就得到了生成的Qclasses。

解决方案2来自@informatik01评论:

我们可以在eclipse.ini中明确指定JVM,如下所示:

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

-vmargs
...

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


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Michael Tontchev
你需要将target/generate-sources/querydsl添加为源文件夹。默认情况下,目标目录中的任何内容都不被视为源文件。 - Robin
@MichaelTontchev http://stackoverflow.com/questions/26833880/including-qclasses-in-the-classpath - Hayi
1
几个更进一步的指针:
  • 如果您正在使用64位JDK,请确保eclipse是64位的。
  • 在重新启动eclipse后,您可能需要进行eclipse clean build。
- Kaushik

5
您可以在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文件中(分行很重要),如此帖子所述,请进行设置。

我尝试了,但仍然得到相同的错误,我认为问题在于“您需要使用JDK运行构建”,而不是“tools.jar”部分。 - Hayi
@Haricha 它们应该是彼此的替代品。你需要使用JDK运行构建或将tools.jar添加到路径中。但是,如果这对你不起作用,你仍然可以尝试调整Eclipse启动选项。在这个线程中有解释。 - eis
你尝试过使用虚拟机选项吗? - eis
当然可以,我通过在命令行中生成源代码来解决这个问题,使用命令mvn generate-sources - Hayi
@eis 你在编辑中提出的建议是正确的。请参见我的评论 - informatik01
我认为在路径中使用 / 时,它对我没有起作用。现在使用 \ 就可以了。 - Hayi

3

我终于做到了!我尝试了很多选项,比如这个这个,但都没有成功。然后我读到了这个评论,真的救了我的命,非常感谢!我按照这个解决方案操作,突然之间就可以运行了!在我的情况下应该被接受为答案。

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


1

<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>

添加插件,问题就会解决。

1
这个问题发生在我身上,因为如上所述,Eclipse本身是通过JRE而不是JDK运行的。
我通过将%JAVA_HOME%\bin添加到我的PATH环境变量的前面来解决了这个问题。
我通过阅读查找Eclipse正在运行的JVM来找出Eclipse使用的JVM。

0

尝试按照以下步骤在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
如有其他问题,请告知。

它不起作用,我更新了我的问题并附加了额外的信息。 - Hayi

0
如果您使用shell命令构建,例如mvn install。那么在shell或cmd窗口中运行此命令:echo $CLASSPATH。此命令将显示您的类路径。
如果您正在使用Eclipse,请打开“窗口”>“首选项”>“Java”>“已安装的JRE”,确保您安装的JRE是jdk根位置。对于我来说,它是C:\ Java \ jdk1.7.0_51
希望能有所帮助。

它没起作用,我更新了我的问题并提供了额外的信息。 - Hayi

0
不要忘记在Eclipse项目设置中检查执行环境的设置:项目构建路径 -> 库 -> JRE系统库

如果这个设置是错误的(例如,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
 ...

希望这可以帮到你。

0

我已经尝试了上面提到的所有方法,但都没有成功。最后,因为我正在设置一台新电脑,我意识到我没有设置JAVA_HOME变量。

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

然后,mvn generate-resources就又开始工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接