Create new file on server with shared directory (java) - java

I am not familiar with Windows shared folders, but here is what I have:
filePath = //server/TEMP/ **(1)**
server = another machine. TEMP = shared folder.
If I open this line with Explorer it goes to
http://server/TEMP/
and I can see list of files and create new.
If I change
filePath = \\server\TEMP\ **(2)**
(slashes replacement) I CAN'T open this directory.
in java code (1.6) I have
File f = new File(filePath);
where filePath can be (1) or (2) - no matters, after creating the File object its .toString() gives me
\\server\TEMP\ (2)
which CAN'T execute .createNewFile() with this exception:
java.io.IOException: The network path was not found at
java.io.WinNTFileSystem.createFileExclusively(Native Method) at
java.io.File.createNewFile(Unknown Source)
How to say to java not to convert my slashes? or how to create file without java.io.File?
Thanks,
Roman.

Related

Java JAR file runs on local machine but missing file on others

The JAR file consists of the ffmpeg.exe file and it can run normally on my machine without any problems. However, if I try to run it on another computer it would tell me that java.io.IOException: Cannot run program "ffmpeg.exe": CreateProcess error=2,The system cannot find the file specified from the stacktrace. The way I imported it was
FFMpeg ffmpeg = new FFMpeg("ffmpeg.exe"); //in res folder
...
//ffmpeg class
public FFMPEG(String ffmepgEXE) {
this.ffmepgEXE = ffmepgEXE;
}
The quick fix is you have to put ffmpeg.exe in the same folder with your .jar file.
If you want to read file from resources folder, you have to change this code:
URL resource = Test.class.getResource("ffmpeg.exe");
String filepath = Paths.get(resource.toURI()).toFile().getAbsolutePath();
FFMpeg ffmpeg = new FFMpeg(filepath);

How to specify a Java file path for both Mac and Ubuntu

I'm using IntelliJ and Spring and Java to locally develop an app on a Mac, and then deploy to a tomcat server on AWS, using Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-1048-aws x86_64).
I'm having trouble specifying the file path so that it works in both environments.
My code is
InputStream fileStream = new FileInputStream("src/main/resources/static/web/data/ReportDates.json");
JsonReader reader = Json.createReader(fileStream);
JsonObject reportDates = reader.readObject();
reader.close();
When I run locally, the file is read in correctly. It is located in:
src/main/resources/static/web/data/ReportDates.json
But when I deploy, that code results in the error message:
java.io.FileNotFoundException: src/main/resources/static/web/data/ReportDates.json (No such file or directory)
The actual location of the file on that machine turns out to be:
/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json
How can I specify the file path so that it works correctly in both environments?
I have given up on using a single path. #Nicholas Pesa got me thinking -- since I use IDEA, I don't have a fixed WEB-INF folder, so it's easier for me to change the path that should be used than to move the file to a fixed location.
My code now uses:
String filepath = (new File("src/main/resources/static/web/data/ReportDates.json").exists()) ? "src/main/resources/static/web/data/ReportDates.json" : "/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json";

Java console app running as a cron job having issue with relative path

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.
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
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.

IOException when creating a temporary file?

I'm creating a task plugin for Atlassian Bamboo. At some moment of task executing, I would like to create a temporary file:
File temp = File.createTempFile(fileName.toString(), null, dir);
temp.deleteOnExit();
, where:
fileName.toString() = e.g. "C:\Atlassian\bamboo-home\xml-data\build-dir\CMPT-CMPTP-JOB1\test.java"
dir = new File("temp");
When testing this locally, everything works fine - the file is created properly. However, after I deploy plugin on server and try to execute above code, I've got an IOException:
java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:1879)
What could be the reason?
Additional info: I'm pretty sure that dir.exists() .
A file name of
"C:\Atlassian\bamboo-home\xml-data\build-dir\CMPT-CMPTP-JOB1\test.java"
is valid on Windows but is invalid on Unix operating systems. You won't be able to create a (temp) file like that, either as specified as the absolute name/path or the file nor just relative to another folder.
If your OS is Windows, you still can't use a full path (starting with drive specification like "C:") to be created as a child of another folder.
You could have spaces in the beginning or the ending of your path, print your file.getAbsolutePath() in order to see the current path where java is reading.
The dir variable must be set with the full (or relative) path to the directory temp. The first arg of File.createTempFile should be the prefix of the temp file (at least three letter long. for exeample "test"). This will create a "test.tmp" in the given directory (specified by the variable dir).
Check the javadoc
You can check existence of the directory dir with dir.exists()

Servlet - Addressing properties files

I have a simple Servlet that needs to pass some properties files to another class.
Properties prop = new Properties();
prop.load(new FileInputStream("/home/user/config.properties"));
Above works fine.
But I can't address the right absolute path in below:
String protocol = prop.getProperty("protocol", "/home/user/protocol.properties");
String routes = prop.getProperty("routes", "/home/user/routes.properties");
MyClass message = new MyClass(protocol, routes, 0);
At the end I receive below from tomcat log:
INFO: Server startup in 3656 ms
java.io.FileNotFoundException: routes.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at com.cc.verticals.Messenger.<init>(Messenger.java:134)
at com.foo.MyClass.<init>(MyClass.java:42)
at com.verticals.cc.util.VerticalUtil.setup(VerticalUtil.java:59)
at com.verticals.cc.util.VerticalUtil.main(VerticalUtil.java:259)
at com.verticals.cc.dao.VerticalDao.<init>(VerticalDao.java:24)
at com.verticals.cc.controller.VerticalController.<init>(VerticalController.java:33)
Line 42 is pointing to the constructor where routes.properties file goes in.
Messenger line 134 points to:
prop.load(new FileInputStream(routesFilename));
Any Idea how to address the properties files and send them as a String parameter? Thanks.
By the looks of it (I prefer if you post the content's of the properties files), there is a property within config.properties such that routes = routes.properties. When you call new file(routes); you get the FileNotFoundException because you are trying to open routes.properties in the current working directory where java was launched (which doesn't exist)
As a side note, you using one property file to reference another property, which is fine but a bit odd or unconventional. Further, you should stick these files in a 'resource' folder to remove absolute paths and gain portability.
Notice that prop.getProperty method cannot throw FileNotFoundException. So that exception must have been thrown earlier on prop.load();
Please make sure that you have opened the permissions on the file. Open a terminal and issue following command:
$ chmod 777 /home/user/routes.properties
$ chmod 777 /home/user/protocol.properties

Categories