'ClassNotFoundException under the same folder in Intellij
I'm calling a Words
class in another java file run
using Java 12. The structure of the folder is as followed. However, I got the ClassNotFoundException
error. Could anyone help me figure out why? I know that I need a classpath for loadClass
to work. How can I do that?
├── App
│ ├── Words.java
│ └── run.java
| └── config.properties
public class run {
public static void main(String[] args) {
Properties config = new Properties();
config.load(new BufferedReader(new FileReader("./config.properties")));
URL pluginsDirUrl = Paths.get(config.getProperty("pluginDirection")).toUri().toURL();
ClassLoader classLoader = new URLClassLoader(new URL[] { pluginsDirUrl },
run.class.getClassLoader());
String wordsProviderClassName = config.getProperty("words");
wordsProviderClassName = removeTail(wordsProviderClassName); //remove the extension of java file e.g Words.java -> Words
Class<?> wordsProviderClass = classLoader.loadClass(wordsProviderClassName);
}
}
The following is my config.properties file
words = Words.java
pluginDirection = ./
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|