如何将Eclipse Java项目移植到另一台PC并从Shell编译?

时间:2012-06-05 08:06:31

标签: java eclipse

我在Eclipse中创建了一个Java项目,并在我的Windows PC上直接从Eclipse成功执行。现在我必须在Linux服务器上运行相同的java程序。

我曾尝试将.class文件从我的PC复制到服务器并运行它,但它没有用。之后我复制了整个项目并从shell运行javac MyProject.java并返回以下错误:

RecordImportBatch.java:2: error: package org.apache.commons.io does not exist
import org.apache.commons.io.FileUtils;

...

RecordImportBatch.java:3: error: package org.neo4j.graphdb does not exist
import org.neo4j.graphdb.RelationshipType;

我猜是因为我没有在编译命令中包含jar文件。

这个项目中包含了很多jar文件,作为一个Java新手到目前为止,我还没有找到编译Eclipse中Eclipse工作项目的方法。

有没有人知道是否有办法直接从Eclipse获取相应的编译命令并将其粘贴到Shell或者我必须手动包含所有罐子吗?如果是这种情况,是否有人知道如何包含所有jar,放在lib目录中,该目录与MyProject.java位于同一文件夹中?

谢谢!

3 个答案:

答案 0 :(得分:1)

如果你刚学习java,这个建议可能会有一些挑战,但是使用maven来构建你的项目会很好,这需要重新组织你的源文件和目录。然后使用assembly插件创建包含所有依赖项的zip。然后运行你的程序,你只需执行以下操作:

unzip myapp.zip
cd myapp
java -cp "lib/*" com.blah.MyApp

(您可能需要调整/ *部分的语法,使用单引号或删除引号,具体取决于您的shell)

这是程序集插件的一个片段(通用...除了版本之外没有任何硬编码,以及遵循约定的路径)。这可以在pom.xml中找到:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/distribution.xml</descriptor>
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <!-- this is used for inheritance merges -->
            <phase>package</phase>
            <!-- append to the packaging phase. -->
            <goals>
                <goal>single</goal>
                <!-- goals == mojos -->
            </goals>
        </execution>
    </executions>
</plugin>

这是一个示例汇编文件(相对于pom.xml,它位于src / main / assembly / distribution.xml中):

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"
>
    <id>${artifact.version}</id>
    <formats>
        <format>zip</format>
    </formats>

    <files>
        <file>
            <!-- an example script instead of using "java -cp ..." each time -->
            <source>${project.basedir}/src/main/bin/run.sh</source>
            <outputDirectory>.</outputDirectory>
            <destName>run.sh</destName>
            <fileMode>0754</fileMode>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/</directory>
            <outputDirectory>/res/</outputDirectory>
            <includes>
                <!-- just examples... -->
                <include>*.sql</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>config/</directory>
            <outputDirectory>/config/</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <excludes>
                <!-- add redundant/useless files here -->
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

另外,eclipse在gui中有一个“jar packager”实用程序,但是几年前我用它时发现它并不是很好。而且我不认为它处理依赖关系,所以你需要在上面加上我的“-cp”参数,并添加所有的jar,或者自己将它们放在你的lib目录中。

还有这个http://fjep.sourceforge.net/但是我从来没有使用过它......我现在只是在快速查找eclipse jar packager时找到了它。在他的教程中,他的最后一行(显示运行它)看起来像:

> java -jar demorun_fat.jar
Hello

答案 1 :(得分:1)

如果您需要做的是在PC上编译并运行Eclipse中的程序并将编译结果传输到Linux机器,然后使用File - &gt;出口 - &gt; Java - &gt; Runnable Jar文件并选择最适合您的包装。

技术上最简单的是使用“将所需的库复制到jar旁边的子文件夹中”,但是您需要通过将文件压缩在一起进行分发,并在Linux机器上解压缩它们。

答案 2 :(得分:-1)

我强烈建议使用任何类型的构建工具,事实上标准为AntMaven,但您可以找到several alternatives。对于一个较小的项目来说,它们都是非常简单的,使用它们也是小菜一碟(请注意,Eclipse也可以生成一个基本的Ant build.xml文件)。

例如,它可以是一个运行整个项目的命令:

> ant run
Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: ...
    [javac] Compiling N source file to ...

run:
     [java] Running application...

main:

BUILD SUCCESSFUL