gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:44:11 32 4
gpt4 key购买 nike

我在 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新手,到目前为止我还没有找到从Shell编译这个项目的方法,这个项目可以在Eclipse中运行。

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

谢谢!

最佳答案

如果你刚开始学习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

关于java - 如何将 Eclipse Java 项目移植到另一台 PC 并从 Shell 编译它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893703/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com