0

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

7
  • If you want to call it you must use java and not javac (which is the compiler and not the runtime). Commented Mar 13, 2017 at 17:38
  • This is a Maven project. It's supposed to be built using Maven. And you ahven't put anything in the classpath in your attempt, so that can't possibly work. You're also confusing compiling java source files, and running a class. Commented Mar 13, 2017 at 17:38
  • @JBNizet How would you compile and run the RecDriver class then? Commented Mar 13, 2017 at 17:40
  • Build the project: using Maven. Run the project: it depends on what kind of project it is and what the maven build produces. Ask your colleagues, which most probably set this project up. Commented Mar 13, 2017 at 17:42
  • @JBNizet I added the Github link to the project. You can have a look at its structure. Commented Mar 13, 2017 at 17:44

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Cannot believe I was looking for a wrong way to do it the whole time. Thanks. It worked.
0

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

2 Comments

How can I see the command which Intellij runs when executing my program? That would help me a lot
In fact it is not possible to see that: stackoverflow.com/questions/2966530/…
0

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>

4 Comments

Are these three separate things to do or they should be done all together? And isn't there easier way to do it? I feel like with every answer from Stackverflow I am going deeper in a harder problem every time and then I should ask about that problem in another thread :)
@HimanAB in theory, you can use them independently of each other, but doing all three will yield the most satisfying results :-)
Can I do just number 3? And what do you mean by set <project><build><finalName>? The artifactId in the pom.xml is librec. So, what exactly should be set to what?
I provided the link to the project. I just want to run RecDriver.java class. Did you try to do it yourself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.