在类路径中包含 Java 1.8 而不指定构建

Including Java 1.8 in classpath without specifying build

我正在和另外两个人一起做一个项目。我们都在同一版本的 Eclipse (Mars.1) 上,但我们的机器上偶尔会有不同版本的 Java 库,这完全是因为我们中的一个人已经升级而其他人还没有(还)。这是暂时的,但显然会导致构建问题。

这是 .classpath 的样子(注意:我在引用 JRE_CONTAINER 的行上手动插入了一个换行符,以帮助您避免滚动该行):

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="res"/>
    <classpathentry exported="true" kind="lib" path="lib/swingx-all-1.6.4.jar"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/
        org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_25]">
            <attributes>
                <attribute name="owner.project.facets" value="java"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="output" path="build/classes"/>
    </classpath>

如您所见,该行指定了构建。是否可以以不包含特定构建的方式指定它?

是的,这也发生在我们身上,解决方案是从代码存储库(Git、SVN 等)中删除 .classpath 文件并将其放入忽略文件列表中( .gitignore 文件或任何你使用的文件)。

同时从每个开发人员的工作区中删除 .classpath 文件,Eclipse 将专门为您的环境重新生成此文件。

这将避免不同次要 java 版本的任何进一步问题。

编辑:既然你提到你没有使用任何构建系统,这里是一个最小的pom.xml,这样你就可以将你的项目转换成一个 Maven 项目:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>your-organization</groupId>
    <artifactId>your-project-or-module-name</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>NameOfTheProject</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- Reference your libraries here -->
        <!-- Maven will download them automatically :O -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这是一个 Introduction to the standard directory layout and here's a guide on Specifying resource directories