簡體   English   中英

如何將Eclipse Java項目移植到另一台PC並從Shell進行編譯?

[英]How to port an Eclipse Java project to another PC and compile it from Shell?

我已經在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中,還是我必須“手動”包含所有jar? 如果是這種情況,是否有人知道如何將所有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包裝器。 在他的教程中,他的最后一行(顯示正在運行)如下所示:

> java -jar demorun_fat.jar
Hello

如果需要做的是在PC上的Eclipse中編譯並運行程序,然后將編譯后的結果傳輸到Linux機器,則使用File-> Export-> Java-> Runnable Jar文件並選擇最合適的包裝為了你。

技術上最簡單的方法是使用“將所需的庫復制到jar旁邊的子文件夾中”,但是隨后您需要通過將文件壓縮在一起進行分發,然后將其解壓縮到Linux盒中。

我強烈建議使用任何一種構建工具, 事實上的標准是AntMaven ,但是您可以找到幾種替代方法 對於一個較小的項目來說,這兩個設置都很簡單,並且使用它們也是小菜一碟(請注意,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM