'Adding Javalin dependencies

I'm trying to use Javalin in my project and I can't seem to understand how to add the needed dependencies in order to work with Javalin without compliation errors. The project i'm working on is not a Maven project, it is a simple Java project so it won't be downloaded automatically. How do I add the dependencies and where? I am using VSCode but can Switch to Intellij IDEA if needed.

Thanks.



Solution 1:[1]

At the risk of pointing you in a direction you may not want to go in... Use a dependency manager (Maven, Gradle, Ivy, or similar). Simple Java projects can be dependency-managed projects, too!

A basic Javalin project includes dozens of dependencies - and dependencies of those dependencies... You will probably have an unpleasant time attempting to handle them all manually, one-by-one.

If you use the Javalin bundle, that will take care of all of this for you.

To give you a sense of what I mean:

enter image description here

If you do decide to use a dependency manager, then your follow-up questions are well covered elsewhere. Or you can ask a follow-up, based on any problems you may encounter.


Update

Yeah but were doing it in a school project and were already half way through the project and now I need to add a Web Client and we don't want to change things all through the project, there's gotta be a way to add those dependencies without creating a new Maven project for it.

You can install Maven and run a command to download all the JARs to a directory.

This is (in my opinion) more work than just using Maven already built into all mainstream IDEs, but here are the steps:

Note: My set-up assumes Windows. You can adjust as needed for Linux or a different OS.

  1. Download Maven - see here.

I downloaded the binary zip archive.

  1. Set up Maven - see here.

Be sure to pay particular attention to the instructions regarding setting the JAVA_HOME environment variable pointing to your JDK installation or having the java executable on your PATH.

I installed my Maven here:

C:\maven\apache-maven-3.8.5

I tested it in a shell using the mvn -v command:

C:\maven\apache-maven-3.8.5\bin\mvn -v
  1. Create a pom.xml file. Maven uses this as its instructions for what to download (and to what location).

In my case I created the POM here:

C:\maven\demo\pom.xml

Its contents are:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    
    <groupId>org.andrewjames</groupId>
    <artifactId>my-Javalin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.javalin</groupId>
            <artifactId>javalin-bundle</artifactId>
            <version>4.5.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>my-Javalin-demo</finalName>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <silent>true</silent>
                            <outputDirectory>C:/maven/demo</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
                        
        </plugins>
    </build>

    
    <name>my-Javalin-demo</name>
</project>

The maven.compiler sections assume I have Java 17 available. You may need to adjust to match your Java version.

The dependencies section is where the javalin-bundle is defined.

The execution section is the directive which causes all dependency JARs to be downloaded to the Maven local repository, and then copied to a new directory.

In my case the new directory will be created here:

C:\maven\demo\target\dependency
  1. Open a CMD shell and cd to C:\maven\demo

  2. At the command line, run the following command:

C:\maven\apache-maven-3.8.5\bin\mvn dependency:copy-dependencies

After that has completed, you will see approx. 100 JAR files in the C:\maven\demo\target\dependency directory.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1