I can't seem to understand how to pass a folder to load files from in the classpath. It works with the text file right in the same folder as the .class file or if I use files/test.txt
instead of test.txt
. What am I doing wrong?
Code:
import java.io.*;
public class T {
public static void main(String[] args) {
String line;
File f = new File("test.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(f));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
}
}
Folders and files:
stuff/T.java
stuff/T.class
Somewhere there is a files folder with the test.txt file which I want to give in the classpath.
I am running the test from the stuff folder in the command line in windows using the command java -cp .../files T
.