0

In my java program, I am trying to save a .csv file into my data folder located in the same folder as the main jar file.

Previously, when I used to run my programs on Windows machines, my relative path was: data\\foo.csv. When I tried the same on Linux, it created and saved the file with name: data\\foo.csv in the root directory.

Then I tried to set the path to data/foo.csv and I am getting this error:

java.io.FileNotFoundException: data/04-12-2015.csv (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileWriter.<init>(Unknown Source)
    at main.Main.saveResultsToFile(Main.java:121)
    at main.Main.main(Main.java:92)

I have set permissions of the directory to 777 (granted all permissions to everyone).

Code responsible for creating and saving the file:

String fileName = "data/foo.csv"
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

Edit:

The permission is not recursive, if that changes anything. Only the data folder has 777 permission.

Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • why don't you use absolute path? e.g. `String fileName= "/root/data/foo.csv"` – nafas Dec 07 '15 at 13:13
  • Maybe because it is a bad approach? What if I move the program, or rename the folder it is located in? – Ondrej Tokar Dec 07 '15 at 13:15
  • you can try something like that within your code to get absolute path: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – nafas Dec 07 '15 at 13:15
  • I don't recommend it because its making it too complex – nafas Dec 07 '15 at 13:16
  • btw, having absolute path is not that uncommon, if you think about it most of installed programs have an absolute path (e.g. in linux ~/.config folder) – nafas Dec 07 '15 at 13:18
  • I think I read somewhere that it might get me the path to the java bin folder or similar rather than path to the actual jar file. – Ondrej Tokar Dec 07 '15 at 13:34

2 Answers2

2

I encountered the same problem today, the post is old but people may end up here so:

The problem is crontab runs from the root directory so relative paths start from the root (/) and get a null point exception. On cronjob you can precede your command with cd $jar.directory

Assume you have your jar file in /home/project/data and want yo run every night:

> crontab -e

> 0 0 * * *  cd /home/project/data && /usr/bin/java -jar program.jar >> log.txt 2>&1
Pardeep
  • 2,153
  • 1
  • 17
  • 37
1

In Java there is a field in File called separatorChar which is a exactly what you need to build platform independent file names. There is also a field called separatorwhich is a String version of the same thing. Makinging a path is then like String fileName = "data" + File.separator + "foo.csv" ;

As the error is file not found and not a complaint about permissions, permissions are not the problem. Presumably then you either are trying to open a file that is not there, or you are have not put the file where cron expects it.

Try the following :

File f = new File( "data" + File.separator + "foo.csv" ) ;

System.err.println( "Path being used is : " + f.getCanonicalPath() ) ;

This should report the resolved pathname that is being used from your relative path name. It should at least tell you where the file is being looked for by your cron job.