简体   繁体   中英

Java: Include external jars while exporting java project (not as a JAR) in eclipse

Now I have searched for sometime for my question and I haven't found my answer yet.

I have a java eclipse project with some external Jars like SWT jars. Now this project is a homework assignment project which I have been working on for some time. I need to turn in the assignment and the professor will review the code and he will also execute it locally.

So how do I export the project by keeping all the dependencies intact. I of course cannot submit a JAR file. I have tried exporting the project as "File System". However it is not preserving the external dependencies.

Is there a way to do this ?

Thanks.

If you don't use management dependency tools such as Maven, you have not the choice : you have to provide all jars needed to make your project compile.
In this case, if you teacher has ANT, an ant build would be a clean way to organize the dependencies.
Otherwise, the manual solution would be to add all external dependencies in an folder of your project. For example : libs . In this libs folder, add all external jars. And then zip/tar the project. The professor has just need to configure the project JDK and it should be ok.

There is a way, with Maven, to download the required dependencies specified in the POM. This will only work if the dependencies you require are on some Maven repository (like central) or if they are installed to your local repository.

Just add a plugin element configuring the maven-dependency-plugin to your POM. This will copy all dependencies to a certain folder during the compile phase. In this case: target/lib.

<project>
    ...
    <build>
        ...
        <plugins>
            ...
            <!-- For copying dependencies into target folder -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        ...
    </build>
    ...
</project>

If you were making a runnable JAR, you could also configure the maven-jar-plugin to add the dependencies copied to target/lib to the Class-Path element in the manifest and use a specified main class.

<!-- For including dependencies in target folder in JAR manfiest Class-Path -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

If using Maven isn't possible, download the JARs and put them in a lib folder with your project. An IDE will be able to pick those up if you tell it where to look. Or just use the javac and java commands with the -cp flag to specify where the dependencies are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM