0

So I worked on a project and i coded everything in eclipse. I had my codes in folder called src and my libraries in a folder called lib. The problem is that, my code needs to be excecuted in the terminal. Well, now i get tons of errors. All of them are because there are libraries missing. I tried to import the .jar file from the lib folder to the src folder but the code still didn't work.

So, how can I "install" the libraries in the terminal?

btw. the library I'm trying to install is the com.google. I've already cloned it with the following line: git clone https://github.com/google/gson

  • How have you built the jar of the project? Was it a maven build? – deHaar Mar 27 '19 at 08:46
  • @deHaar this is where I found how to clone it: https://stackoverflow.com/questions/37975605/how-do-i-download-the-gson-library –  Mar 27 '19 at 08:48
  • OK, it is a maven build... You can tell maven to include this library into the jar of your project, but that is not explained within a few minutes. You have to use a maven plugin (maybe the shade plugin) and create a jar that includes the gson library. The jar will be a lot bigger, but will not look for libraries where there are none. – deHaar Mar 27 '19 at 08:51
  • Are you using Maven or Gradle to install dependencies? Or you simply put all needed library in your lib/ folder ? – Boris EKUE-HETTAH Mar 27 '19 at 08:51
  • @deHaar thank you! Do you have any links to a tutorial on that? Because this sounds quite difficult to do. –  Mar 27 '19 at 08:52
  • @BorisEKUE-HETTAH well i worked on Eclipse so i just put the .jar files in the lib folder and then added them to the project in the settings. And everything worked on eclipse. –  Mar 27 '19 at 08:54
  • If you have problems with gson you can find a jar of it here https://jar-download.com/artifacts/com.google.code.gson/gson/2.8.2/source-code – Boris EKUE-HETTAH Mar 27 '19 at 08:59
  • @youneazy The fact that it is working in eclipse does not necessarily mean your compiled jar will work, too. If you have just put the libraries into the lib folder and told eclipse to use them, then you might not have a maven project (the gson clone is a maven project). Get familiar with maven, it is just awesome (though I just know approximately 10% of its features myself). See [maven itself](https://maven.apache.org/) and the use of [the shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/). I think you can even tell maven to fetch the gson library from a server. – deHaar Mar 27 '19 at 08:59

2 Answers2

2

First of all, you have to make your project to compile perfectly with your IDE or whatever.

Later, you should create the runnable JAR file (with eclipse if you want, but it's important the world RUNNABLE jar file. Click in your project > Export > runnable jar file and select you main class in "Launch configuration".).

Finally, go to the console to the path of your JAR file and execute

java -jar file.jar

I think it should work :)

randomkwiz
  • 36
  • 3
0

If you want to build your project from terminal, you need to make sure of two things. 1. You should have main class attribute in mainfest file. 2. Jar should be compiled with all the dependencies. (This will result in a bigger jar though)

You can do both with maven assembly plugin. Add the following in build/plugins in your pom.xml

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.tanmayvijayvargiya.MainApp</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Update the with your mainClass

To build the jar, run mvn package. This will generate jar in your target folder. Next, to run the jar run java -jar target/jar-name-with-dependencies.jar

Source Maven Assembly Plugin Usage

Tanmay_vijay
  • 569
  • 3
  • 12
  • Thanks for your help! It already worked with the help of randomkwiz. But still, I appreciate the work! –  Mar 27 '19 at 09:58
  • No problem! This might help someone who don't want to use ide at all for building or during deployment pipeline. – Tanmay_vijay Mar 27 '19 at 10:04