Reputation: 273
I have build a project. Project must read a configuration file and data file (both text files). Files shoud be uploaded by user, they can have different names. Everything works.
I created runnable jar from project and trying to run it from cmd like this:
java -jar walksat.jar "params_3.txt", "Test3ArielWCNF-log.wcnf"
.
I can print the arguments in the jar, it sees them OK, but I get:
java.io.FileNotFoundException: params_3.txt, (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at mla.project.main.WalkSAT.setGlobalParams(WalkSAT.java:347)
at mla.project.main.WalkSAT.main(WalkSAT.java:60)
Same error I get for second file.
But if I'll write file's names hardcoded (instead of args[0] and args[1]) jar can read them.
The jar file and other two files is in same folder.
What am i missing here?
Update args[0]
has ,
in the end and I didn't see it.
Upvotes: 0
Views: 521
Reputation: 2288
File jarFile = new File(".");
This will get you a reference of the current directory where the jar is residing. You can now read the files from this using jarfile.listFiles(). The assumption is that you should keep the configuration ,the data file and the jar file in the same folder
I have created a small main class which you can export as a runnable jar and run it. It will list all the files in the directory.
import java.io.File;
public class Reader {
public static void main(String[] args) {
File jarFile = new File(".");
if(jarFile.isDirectory())
{
File[] allFiles = jarFile.listFiles();
System.out.println("Printing File names");
for (File file : allFiles) {
System.out.println(file.getName());
//code for reading the file goes here..............
}
}
}
}
Upvotes: 2