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.
You are running the Java compiler from the wrong directory.
It seems you are running the
javac
command from within thejexample
directory. This isn't made clear, but this can be deduced from the directory structure and the fact that you are compiling theMain
class usingjavac .\Main.java
. You aslo say that the code compiles in IntelliJ. If this is the case then all of the classes must be thejexample
package.When the Java compiler starts compiling your
Main
class and needs to find theRunnableKeyValueFactory
class in thejexample
package, it looks for the fileRunnableKeyValueFactory.java
within thejexample
subdirectory of the current directory. Because you are runningjavac
from within thejexample
directory, the Java compiler is looking for this class inI 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.