Problems with RunnableInterfaces in Java JDK-20

61 views Asked by At

I have problems to import an interface in Java. In intellj I can execute the code despite errors. When I do it with Java itself it does not work. Can you help me?

This is my interface.

package jexample;

import java.util.Vector;

public interface RunnableInterface {
    public void run(Vector<String> args);
}

This is my MyCryptMain.java:

package jexample;

import java.util.Vector;

public class MyCryptMain {

    static final String PROG_NAME = "MyCryptMain";
    static final String SCOPE = "jexample.";

    public static final String[][] MY_ARRAY = {
            {"-genkeys", SCOPE + "RunGenKeys"},
            {"-encrypt", SCOPE + "RunEncrypt"},
            {"-decrypt", SCOPE + "RunDecrypt"},
            {"-copy", SCOPE + "RunCopy"}
    };

    public static void usage() {
        String msg[] = {
                "Program '" + PROG_NAME + "'",
                "usage:",
                "\t" + PROG_NAME + " " + MY_ARRAY[0][0]
                        + " [priv_keyfile] [pub_keyfile]",
                "\t" + PROG_NAME + " " + MY_ARRAY[1][0]
                        + " [pub_keyfile] [ifile] [ofile] ",
                "\t" + PROG_NAME + " " + MY_ARRAY[2][0]
                        + " [privkeyfile] [ifile] [ofile] ",
                "\t" + PROG_NAME + " " + MY_ARRAY[3][0]
                        + " [dummyword] [ifile]  [ofile] ",
                ""
        };

        for (int i = 0; i < msg.length; ++i) {
            System.err.println(msg[i]);
        }

        System.exit(0);
    }

    public static void main(String[] args) throws Exception {
        RunnableKeyValueFactory runnableKeyValueFactory = new RunnableKeyValueFactory(MY_ARRAY);

        int argSize = args.length;

        if (argSize <= 0) {
            usage();
        }

        String cmdKey = new String(args[0]);

        if (!runnableKeyValueFactory.containsKey(cmdKey)) {
            usage();
        }

        Vector<String> optArgVector = MyUtils.shiftArgs(args, 5);

        RunnableInterface myRun = runnableKeyValueFactory.getInstanceFromKey(cmdKey);

        myRun.run(optArgVector);

        System.out.println("Ende des Programms.");
    }
}

This is my folder structure:

C:.
\---jexample
        MyCryptMain.java
        MyUtils.java
        RunCopy.java
        RunDecrypt.java
        RunEncrypt.java
        RunGenKeys.java
        RunnableBase.java
        RunnableFactory.java
        RunnableInterface.java
        RunnableKeyValueFactory.java

As you can see, all files are in the right place and can be found.

javac .\MyCryptMain.java
.\MyCryptMain.java:56: error: cannot find symbol
RunnableKeyValueFactory runnableKeyValueFactory
^
symbol: class RunnableKeyValueFactory
location: class MyCryptMain
.\MyCryptMain.java:57: error: cannot find symbol
= new RunnableKeyValueFactory(MY_ARRAY);
^
symbol: class RunnableKeyValueFactory
location: class MyCryptMain
.\MyCryptMain.java:75: error: cannot find symbol
Vector<String> optArgVector = MyUtils.shiftArgs(args, 5);
^
symbol: variable MyUtils
location: class MyCryptMain
.\MyCryptMain.java:78: error: cannot find symbol
RunnableInterface myRun
^
symbol: class RunnableInterface
location: class MyCryptMain
4 errors

Nevertheless, java (javac) does not find the interface. Can you help me please?

Greetings Liffecs.

I expected java to find the interface and continue executing the program normally. In Intellj with OpenJDK 20 it also works without problems.

1

There are 1 answers

0
Luke Woodward On

You are running the Java compiler from the wrong directory.

It seems you are running the javac command from within the jexample directory. This isn't made clear, but this can be deduced from the directory structure and the fact that you are compiling the Main class using javac .\Main.java. You aslo say that the code compiles in IntelliJ. If this is the case then all of the classes must be the jexample package.

When the Java compiler starts compiling your Main class and needs to find the RunnableKeyValueFactory class in the jexample package, it looks for the file RunnableKeyValueFactory.java within the jexample subdirectory of the current directory. Because you are running javac from within the jexample directory, the Java compiler is looking for this class in

C:\-----\jexample\jexample\RunnableKeyValueFactory.java

I suspect this file does not exist. Hence the compiler cannot find your RunnableKeyValueFactory class.

Try changing to the parent directory and running javac .\jexample\MyCryptMain.java. Your code should compile from there, or if it doesn't, you should get different compiler errors.

Note that the Java compiler won't automatically detect that you are in a subdirectory whose name matches the package and automatically base its searches for classes from the parent directory.