HimanAB
HimanAB

Reputation: 2573

Setting classpath for a java project with many libraries and external resources in command line

I have the following project. It has some property files in the conf folder, some data in the data folder, some jar files in the lib folder and also some external libraries that are not shown in the photo due to size limitation. Imagine I want to run the RecDriver class. How exactly should I set the classpath so that I can run it in command line? This is how I did it but it does not work as it cannot fine some other files in the project.

C:\Users\myUserName\Downloads\librec-2.0.0\librec-2.0.0\core\src\main\java\net\librec\tool\driver>  javac RecDriver.java

The project can be downloaded here:

https://github.com/guoguibing/librec

project structure

Upvotes: 0

Views: 1181

Answers (3)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

Three things to do:

  1. Use the Maven Shade Plugin to create a fat jar (jar with dependencies)
  2. Use the Maven-Jar-Plugin to make the Jar executable
  3. Set <project><build><finalName> to ${artifactId}

Now, after your build ran successfully, you can run your app with

java -jar target/YourArtifactId.jar

(Substitute your project's artifactId for "YourArtifactId")


Okay, here's the full setup. Add a build section like this to your pom.xml (merge it with any existing one).

<build>
    <plugins>
      <!-- number 1 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <minimizeJar>true</minimizeJar>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- number 2 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>

    <!-- number 3 -->
    <finalName>${project.artifactId}</finalName>
</build>

Upvotes: 0

minus
minus

Reputation: 2786

You can use bin/librec or bin/librec.cmd to run it from commandline.

If you want to build your launch command you can see those start scripts and adapt them for your purposes.

Upvotes: 1

Edu G
Edu G

Reputation: 1070

To run your app through command line, once you have the .class files in some dir (usually build) all you have to do is run your application with java -cp "path where jvm can find every .class that you project needs" MainClass.

The -cp flag only tells where to look for compiled .class files, since you are using IntellIJ you can see the command it runs when executing your program, there is a class path that it uses.

Class Path points to where your .class files are, they can be in separate folders, but you need to include every dir when giving the class path, separated by ";"

Example taken from another question in SO.

java -cp "Test.jar;lib/*" my.package.MainClass

Upvotes: 0

Related Questions