-1

The instruction on the javalin website are not clear to me. Maybe I'm just slow but, it seems that I'm missing a couple of parts to the puzzle that the website doesn't make obvious. For example, to install flask once you have python installed all you do is type "pip install flask" in the command line and thats it. If you don't have pip then install pip then flask as above. pretty strait forward stuff.

For javalin it says just add the dependencies code to maven. Sooo, I guess I have to download maven? There are instruction for eclipse and intellij so, does that mean that maven is bundled with those 2 IDEs? What if I want to use visual studio code? Do I really have to download another IDE? Anyways, if I download and install maven then are there additional files to download? You would think you need the javalin related files. There is also a zip file but where do I put it? where the sun don't shine? I think this is one of those things were if you are accustomed to this side of development then its pretty strait forward but for newbies and hobbyist not so much.

Long story short can someone give me some step by steps?

Geo
  • 23
  • 9

1 Answers1

2

There are various ways to start using Javalin - here is one (it's the way I started - and yes, it was on Windows 10).

I happen to use Apache NetBeans (v 11.1 currently) as my Java IDE most of the time, but it's a similar process with Eclipse (and others, I'm sure). It also helps if you have some experience with Maven - but Maven is built into NetBeans (and Eclipse), so you don't have to download Maven separately to get started.

Assuming NetBeans (and a reasonably recent version of Java - I am using 11):

In NetBeans:

1) Select File > New Project.

2) For "Categories" choose "Java with Maven".

3) For "Projects" choose Java Application".

4) Click on "Next".

5) For "Project Name", choose whatever name you like (or leave the default name, which will be "mavenproject1" or something like that).

6) For "Group ID" I tend to use "org.me" - whatever you want.

7) Click on "Finish".

8) In the newly created project (left hand side of the IDE in the "Projects" tab), open the "Project Files" folder. There will be a new "pom.xml" file in there.

9) Double-click on the file to open it. This is where you "just add that Maven dependency" that the Javalin site mentions. It needs to be added to a new <dependencies> section.

You will also need to ADD THE SLF4J dependencies - see the example below - to support logging.

Here is a typical new POM file at this stage:

<?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>
    <groupId>org.me</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>io.javalin</groupId>
            <artifactId>javalin</artifactId>
            <version>3.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.8.0-beta4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.28</version>
        </dependency>

    </dependencies>

</project>

10) Choose Run > Build Project. You haven't written any code yet, but this will cause Maven to download the Javalin JAR file - and all its dependent JARs. You can see them all in your project's "Dependencies" folder.

There are more than 20 JARs downloaded. For example, a bunch of Jetty JARs - because Javalin uses an embedded Jetty web server behind the scenes.

11) Create a new Java class called "HelloWorld".

12) You can paste in the hello world example from the Javalin web site:

import io.javalin.Javalin;

public class HelloWorld {
    public static void main(String[] args) {
        Javalin app = Javalin.create().start(7000);
        app.get("/", ctx -> ctx.result("Hello World"));
    }
}

13) Run the project in NetBeans (or your IDE of choice). You should see the following terminal output - or similar:

[main] INFO io.javalin.Javalin - 
           __                      __ _
          / /____ _ _   __ ____ _ / /(_)____
     __  / // __ `/| | / // __ `// // // __ \
    / /_/ // /_/ / | |/ // /_/ // // // / / /
    \____/ \__,_/  |___/ \__,_//_//_//_/ /_/

        https://javalin.io/documentation

[main] INFO org.eclipse.jetty.util.log - Logging initialized @208ms to org.eclipse.jetty.util.log.Slf4jLog
[main] INFO io.javalin.Javalin - Starting Javalin ...
[main] INFO io.javalin.Javalin - Listening on http://localhost:7000/
[main] INFO io.javalin.Javalin - Javalin started in 311ms \o/

14) Open a browser and browse to http://localhost:7000/ - you should see your "hello world" message.

(If port 7000 is already in use, then edit your source code to choose a different port.)

That really is the barest-bones way to get started.

I see that the question was down-voted - probably because it's really more of a Maven question than a Javalin question, at heart.

But I hope this helps. I have enjoyed using Javalin - hope you do too.

andrewJames
  • 19,570
  • 8
  • 19
  • 51