Search code examples
javamavenlanterna

What do I need to compile this lanterna maven project from terminal?


I'm following a simple lanterna demo tutorial using maven. The problem is that the program runs perfectly using IDE intellij, but wont compile from terminal. Do I need to include something more in my pom.xml file?

My file Rouge.java opens a simple window with hello world. Error message when trying to compile file from terminal (javac Rouge.java) includes error: package com.googlecode.lanterna.terminal does not exist.

I thought the problem could be that the packages does not compile and tried to add different maven compiler dependencies but the problem remains.

import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;

import java.io.IOException;

public class Rogue {


    public static void main(String[] args) throws IOException {
        Terminal terminal = new DefaultTerminalFactory().createTerminal();
        Screen screen = new TerminalScreen(terminal);

        TextGraphics tg = screen.newTextGraphics();
        screen.startScreen();

        //write a text
        tg.putString(10, 10, "Hello World!");
        screen.refresh();

    }


}

<?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>fwiman</groupId>
    <artifactId>lanternademo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.googlecode.lanterna</groupId>
            <artifactId>lanterna</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>





</project>


I want to be able to run the program from terminal


Solution

  • When you run javac/java command on Rogue.java it tries to look for lanterna-3.0.1.jar dependency which is not be available or neither set in the classpath. With the current pom.xml you shared you will still get java.lang.ClassNotFoundException: com.googlecode.lanterna when you execute(mvn install) it on the .jar file. There are couple of options you could look:

    • you can define maven-assembly-plugin to your build tag in pom.xml. This will create a fat jar which includes all the dependencies
    • if you are in windows you can create .bat script and include your necessary dependency and run the java command. Something like below

      TITLE lanterna demo set classpath=.\lib\* java com.demo.Rogue