I am building an application that takes input through an XML file and executes a number of operations based on it. I know the application works, because I can supply a file for an XML directly in code and it will run as it should.
The problem arises when I try to pass the path to an XML file in through the commandline args of the packaged .jar.
My main looks like this:
public static void main(String[] args) {
LogWriter.openWriter();
LogWriter.log("Main start.");
Controller controller = new Controller();
LogWriter.log("Controller created.");
List<Parameters> parameters = new ArrayList<Parameters>();
for (int i = 0; i < args.length; i++) {
LogWriter.log("Got arg: " + args[i]);
File file = new File(args[i]);
LogWriter.log("Parameter file: " + file.getAbsolutePath());
if (file.exists()) {
LogWriter.log("File exists. Getting parameters.");
Parameters para = controller.getParameters(file);
LogWriter.log("Attempting to add parameters.");
parameters.add(para);
LogWriter.log("Added parameters.");
} else {
LogWriter.log("File is not valid.");
}
}
}
The script I use to run it looks like this:
java -jar PDFRedacter.jar C:\Users\je\Downloads\XMLConfig.xml
What is really strange about is, is that it only fails when I give it a valid file. If I supply an invalid file, it runs, and logs that the file is invalid, and as mentioned it also runs if I input a valid file directly in the code.
However, with a valid path written in the batch, not even the first 'Main start' is logged, so the code does not run at all. Is this perhaps some access problem?
EDIT: The logger is not my issue, it works as intended both with and without a valid file. Example of 'Invalid file' log:
Main start.
Controller created.
Got arg: C:\Users\je\Downloads\XMLConfig
Parameter file: C:\Users\je\Downloads\XMLConfig
File is not valid.
To clarify, the issue is not actually reading a file. The issue is, that when I supply a valid file through the batch script, it seems like my code doesn't run at all.
FINAL EDIT:
It turns out the problem was simply with the packaging of my .jar, lacking a dependecy. As someone else suggested, my logger was only flushing at the very end of excecution, and that's why it didn't write anything. Thanks for your comments.
As Thomas suspected, there must be another reason for the program to fail, because your function should work. If you reduce it to detecting if a file exists and once you made shure it works, add the parameter list, controller and logger one after another you may find the reason for the failure.ยด
Here is the example I tested - and it works perfectly for all existing files or directories.
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("Got arg: " + args[i]);
File file = new File(args[i]);
System.out.println("Parameter file: " + file.getAbsolutePath());
if (file.exists()) {
System.out.println("File exists. Getting parameters.");
} else {
System.out.println("File is not valid.");
}
}
}
Related
I'm working on a program that reads from a file with a custom extension I made. The idea is that an error report is created every time a file is read. The error report must be in whatever folder the source file was called from. The error file is a copy of the source file, but it has a line number at the beginning of each line and indicates at the end of the line if an error occurred at that line.
(I'm not trying to set up the numbering on this question, this question is just about creating the copy)
So for example, when I call my program from the command prompt:
C:\MyLocation>java =jar myJavaProgram.jar myFileToRead.CustomExtension
Asides from reading the file, it should also create a copy at the same location called myFileToRead-ErrorReport.txt
Additionally: If the source file has no extension, I have to assume that it's still the correct extension, so there won't always be a '.myCustomExtension' segment to replace into .txt
The problem is that I don't know how to grab the file name, because it's coming from the args list of the main method. I am using the following to read the file
public static void main(String[] args) throws FileNotFoundException {
try{
File inputFile = new File(args[0]);
Scanner sc = new Scanner(inputFile);
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
System.out.println("File not found.");
}
}
So how can I get that file name to make something like
File errorReport = new File("./" + inputFileName + ".txt"); ?
First the code. The explanations appear after the code.
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("ERROR: Missing filename argument.");
}
else {
String filename = args[0];
if (filename.length() == 0) {
System.out.println("ERROR: Empty filename argument.");
}
else {
if (!filename.endsWith(".CustomExtension")) {
filename += ".CustomExtension";
}
String name = filename.substring(0, filename.indexOf(".CustomExtension"));
name += "-ErrorReport.txt";
File inputFile = new File(filename);
File directory = inputFile.getParentFile();
File errorReport = new File(directory, name);
System.out.println(errorReport.getAbsolutePath());
}
}
}
I make it a habit of checking the parameters. Hence I first check that the file name was supplied. If it was, then I check that it is not an empty string. Note that I have omitted some checks, for example checking whether the named file exists and is readable.
You wrote in your question that the file name argument may or may not include the .CustomExtension. Hence I check whether the supplied name ends with the required extension and append it if necessary. Now, since I know what the file name ends with, that means that the required part of the name is everything up to the extension and that's what the call to substring() gives me.
Once I have the required name, I just append the part that you want to append, i.e. -ErrorReport.txt.
Method getParentFile() in class java.io.File returns the directory that the file is located in. Hence I have the directory that the input file is in. Finally I can create the error report file in the same directory as the input file and with the desired file name. For that I use the constructor of class java.io.File that takes two parameters. Read the javadoc for details.
Note that creating a File object does not create the file. Creating an object to write to the file does, for example FileWriter or OutputStreamWriter.
Here is the code example to create a file, with filename passed from cmd line as argument and to get the same file name :
Class Demo{
public static void main(String[]args){
String path ="<path of file>"
String name= args[0];
File f = new File(path+name+".txt");
f.createNewFile(); //create file
System.out.println(f.getName()); // will give you the file name
}
}
cmd line : java -cp . Demo.java <filename>
Note : '.' used in the cmd if your class file is present in current dir
You can refer the code and modify to suit your requirement.
Hope this is what you are looking for.
I have a problem when reading a text file in java. The class is FlashCardReader and I have the following constructor that handles the part of the reading.
public FlashCardReader( String fileName ) {
try{
reader = new BufferedReader(new FileReader(fileName));
}catch(FileNotFoundException e){
System.out.println("The file was not found or the name may be wrong!");
}
}
My main method looks like this:
public static void main(String[] args) {
FlashCardReader fcr = new FlashCardReader("Questions.txt");
}
And the final output is: The file was not found or the name may be wrong!
Some help would be greatly appreciated, cheers!
You can print the current directory of your java program where it is executed from with this java code,
System.out.println("CurrentDir: " + (new File(".").getCanonicalPath()));
Say it prints,
CurrentDir: D:\pkr\test
Then you can correctly choose a path through which your file can be correctly located.
Most likely, your src folder should be in test directory and in that case you can either move your file from src folder to test folder or refer your file in your code like this,
..\\Questions.txt
which should be able to read your file.
Let me know if this works.
I'm working on a mod to generate random plants in Unreal World. I posted the initial version of the mod on the game's forum, and I've been told about an issue with the Windows version of the mod. The problem is specifically with the name generation; it just returns null for each name. The user who reported this problem to me posted this stacktrace (it's the same error repeated several times; no need to read them all):
This is the method where the error is occurring:
private static String getName(Random rn, boolean full) {
try {
String name;
int prefix = rn.nextInt(179);
int suffix = rn.nextInt(72);
Scanner sc1 = new Scanner(new File("srcwin\\Prefixes.txt")); // error occurs on this line
for (int i=0; i<prefix; i++) {
sc1.nextLine();
}
name = sc1.next();
if (name.contains("'")) {
name += " ";
}
if (full || rn.nextInt(3) == 0) {
if (rn.nextInt(3) == 0 && !name.contains(" ")) {
name += " ";
}
Scanner sc2 = new Scanner(new File("srcwin\\Suffixes.txt"));
for (int i=0; i<suffix; i++) {
sc2.nextLine();
}
name += sc2.next();
}
return name;
} catch (FileNotFoundException ex) {
Logger.getLogger(UrWPlantMod.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
This program is contained in a JAR containing the directories modwin, srcwin, and META-INF. The class files are contained in modwin, while the source code and txt files are contained in srcwin. The user who reported this problem is running Windows 10 Home (Version 1511, Build 10586). Another user, running Windows 8.1 (no other specifics given), can run this just fine, with no FileNotFoundExceptions or null names.
If it's relevant, I am running Ubuntu 14.04, with the only difference in this code being that the file path is src/Prefixes.txt instead of srcwin\\Prefixes.txt, and it runs just fine for me.
If you'd like to see the other lines of code mentioned in the stacktrace:
berries[i] = new Plant(getName(rn, false) + "berry " + getBerryName(rn), rn.nextInt(4)+1, getImg(rn, "berry"));
and
createBerries(rn); // the above line of code is in the method called here
new Scanner(new File("srcwin\\Prefixes.txt")) will open the file srcwin\Prefixes.txt from the current directory.
Directories inside a Jar file cannot be access this way.
So, either the current directory is not what you think it is, or the files are not there (in folder srcwin on the file system).
To load content of a file inside your Jar (which is on the classpath, we presume), use getResourceAsStream().
try (Scanner sc1 = new Scanner(MyClass.class.getResourceAsStream("/srcwin/Prefixes.txt"))) {
// code here
}
Note the change to the file name. It starts with / and uses /.
Also note that you should always close the Scanner when done with it (except when used with System.in), hence the try-with-resources block.
I'm getting a file not found exception from this code even though it's within the try catch statement and I'm not sure what's wrong, the file is within the project folder and is called 'someFile.txt'. This is the main method:
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("no arguments given");
return;
}
double FRE = sortFile(args[0]);
System.out.println("Readability of file " + args[0] + "= " + FRE);
}
And this is the sortFile method where the exception occurs:
public static double sortFile(String FileName) {
int nWords = 0;
int nSyllables = 0;
int nSentences = 0;
File text = new File(FileName);
try {
Scanner sc = new Scanner(text);
while (sc.hasNext()) {
contents.add(sc.next());
++nWords;
}
sc.close();
for (String e : contents) {
getNumSyllables(e);
}
} catch (FileNotFoundException e) {
System.out.println("The file" + FileName + "could not be opened.");
e.printStackTrace();
}
double FRE = getFRE(nWords, nSyllables, nSentences);
return FRE;
}
Thanks for any help :)
well, the file does not exist in that location. Try to add
System.out.println(text.getAbsolutePath())
to see where the file is expected. Note, when you provide a relative path (e.g. some/path/filename.ext), this is relative to the working directory. The working directory is the folder your java program is started in.
If you're using an IDE (e.g. Eclipse, IntelliJ, Netbeans) you can define the working directory in your run configuration.
See:
Javadoc of java.io.File to learn how relative paths work inside a Java environment: http://docs.oracle.com/javase/7/docs/api/java/io/File.html
working dir: Getting the Current Working Directory in Java
I'm getting a file not found exception from this code even though it's
within the try catch statement
The try-catch does not prevent the Exception from being thrown. It merely executes the code in the catch block when an Exception is thrown, and you are just printing the stack trace in the catch block, which is what usually printed anyways on uncaught exceptions.
To resolve your actual issue, first try passing the full path to the file, verify that it works and then use Tim's answer to debug your absolute path.
Try launching your program with the absolute path.
java yourclassname absolutepath_to_someFile.txt
If I do this:
File f = new File("c:\\text.txt");
if (f.exists()) {
System.out.println("File exists");
} else {
System.out.println("File not found!");
}
Then the file gets created and always returns "File exists". Is it possible to check if a file exists without creating it?
EDIT:
I forgot to mention that it's in a for loop. So here's the real thing:
for (int i = 0; i < 10; i++) {
File file = new File("c:\\text" + i + ".txt");
System.out.println("New file created: " + file.getPath());
}
When you instantiate a File, you're not creating anything on disk but just building an object on which you can call some methods, like exists().
That's fine and cheap, don't try to avoid this instantiation.
The File instance has only two fields:
private String path;
private transient int prefixLength;
And here is the constructor :
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
As you can see, the File instance is just an encapsulation of the path. Creating it in order to call exists() is the correct way to proceed. Don't try to optimize it away.
Starting from Java 7 you can use java.nio.file.Files.exists:
Path p = Paths.get("C:\\Users\\first.last");
boolean exists = Files.exists(p);
boolean notExists = Files.notExists(p);
if (exists) {
System.out.println("File exists!");
} else if (notExists) {
System.out.println("File doesn't exist!");
} else {
System.out.println("File's status is unknown!");
}
In the Oracle tutorial you can find some details about this:
The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists, or does not exist. You can do so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). When you are testing a file's existence, three results are possible:
The file is verified to exist.
The file is verified to not exist.
The file's status is unknown. This result can occur when the program does not have access to the file.
If both exists and notExists return false, the existence of the file cannot be verified.
Creating a File instance does not create a file on the file system, so the posted code will do what you require.
The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.
This can be applied too for Files.noExists, Files.isDirectory and Files.isRegularFile
According this you can use the following :
Paths.get("file_path").toFile().exists()