Java

How to build package or program using maven

In order to build a package using maven, run

mvn package

If you also want to install the maven package in the local maven repository (usually in ~/.m2), use

mvn install

In case you don’t have Maven installed, you can install it on Ubuntu using

sudo apt install maven

 

Posted by Uli Köhler in Build systems, Java

How to fix ‘cannot find symbol’ in Java

Problem:

You want to compile some Java source code (e.g. using Maven), but you get an error message like

[ERROR] /home/user/myproject/src/main/java/com/mydomain/myproject/Main.java:[123,40] cannot find symbol
[ERROR] symbol: class MyClass

Solution:

Java does not know where to find MyClass.

First, check if you have imported MyClass correctly.

If MyClass can be found in your library, you most likely are missing an import statement at the top of the file where the error occurs.

If, on the other hand, MyClass is imported from an external library, check if:

  • You have the correct version of the library. Possibly, you are using an old version of the library where MyClass is not present. This is often the case when using SNAPSHOT versions in maven since different developers might have different SNAPSHOTs so one might have issue building while another might not.
  • You are using the correct import statement (refer to the docs or the source code of the library to check if you are using the correct package.

If you don’t know in which library you can find a certain symbol in, see our post [To be done].

What exactly are ‘symbols’?

The concept and term of a symbol is used in many different programming languages.
Basically it means ‘a name that refers to something declared somewhere else in more detail’.

Therefore, if you encounter error messages like ‘cannot find symbol’, the compiler is trying to tell you: “I don’t know what that name refers to”.

Example:

When you declare a class in Java, e.g.

class MyClass {
    /* Your code goes here ! */
}

you can later refer to that class by its name, MyClass, e.g. in

MyClass class = new MyClass();

In that line of code, MyClass is used symbolically to refer to the full class declaration (class MyClass { /* ... */}) which we listed before.

Hence, MyClass is used as a symbol to refer to your previous (full) declaration of MyClass.

If the name MyClass, however, has no associated full declaration, the compiler will tell you ‘cannot find symbol’.

Posted by Uli Köhler in Java

Othello in Java: Part 1: Data structures

You’ve got a big problem. Someone forces encourages you to implement a complete Othello UI+AI in Java, but you don’t have any idea how to do that. If you already know how to implement the basics and you are interested in more advanced strategy concepts, you might be interested in the other parts of this series (yet to come).

In this multi-part series I will not provide any complete solution to any of the standard Othello tasks. Instead, I will provide (hopefully) helpful hints how to get your coding going and explain how your code works.

Continue reading →

Posted by Uli Köhler in Java

Check Scanner number input boundary in Java

In many cases if you want to create an interactive command line interface, you need to check if a number entered by the user is valid and – if it isn’t – you want the user to re-input it.

Here’s a simple static method to check if a number typed by a user is within a given boundary:

/**
 * Lets the user input an integer value until it satisfies the given
 * conditions
 *
 * @param msg The prompt to ask the user for the value.
 * @param lower The lower boundary, inclusive
 * @param upper The upper boundary, inclusive
*/
private static int guardedInput(String msg, int lower, int upper) {
    Scanner scanner = new Scanner(System.in);
    while (true) {
        System.out.print(msg);
        int val = -1;
        try {
        val = scanner.nextInt();
        } catch (InputMismatchException ex) {
        System.out.println("Illegal value: Please type a number!");
        }
        if (val < lower) {
        System.out.println("Illegal value: Must be greater than " + (lower - 1));
        } else if (val > upper) {
        System.out.println("Illegal value: Must be smaller than " + (upper + 1));
        } else {
        return val;
        }
    }
}

 

Posted by Uli Köhler in Java