In my jar application I do some calculations in exe program. When files and program.exe was in the same dir I used this command:
String[] str={"program.exe", "file1.txt", "file2.txt"};
pr = rt.exec(str);
and it worked great. But when I moved files to other dir and I try use this command:
String[] str={"program.exe", "temp\\file1.txt", "temp\\file2.txt"};
pr = rt.exec(str);
program.exe doesn't see files. What is more odd it start to see files when I change its names for anything else that default. file1.txt, file2.txt and temp are created in my jar program before program.exe start.
edit:
When problem started I try sth like this: default names file1.txt and file2.txt, I changed to aaa.txt and bbb.txt (in windows), and then:
String[] str={"program.exe", "temp\\aaa.txt", "temp\\bbb.txt"};
and it works.
edit2:
Now I know that problem is in program.exe. When I use it from command line (not from jar), like this:
program.exe temp\file1.txt temp\file2.txt
error:
FANN Error 1: Unable to open configuration file "temp\file1.txtÉ║#" for reading.
fann is artificial neural network library. When I copy files to program.exe dir:
program.exe file1.txt file2.txt
it works! When I changed files names in temp and do:
program.exe temp\file1aaa.txt temp\file2bbb.txt
it works also! So it is fann lib bug?
I'd use the ProcessBuilder api (it gives you much more control than Runtime.exec()) and I'd also use absolute paths:
File directory = new File("/path/tp/program.exe's/parent");
int returnCode = new ProcessBuilder("program.exe",
new File(directory, "temp/file1.txt").getAbsolutePath(),
new File(directory, "temp/file2.txt").getAbsolutePath()
)
.directory(directory).start().waitFor();
Give complete path of the filename and see. Something like below
String[] str = {"program.exe", "D:\\temp\\file1.txt", "D:\\temp\\file2.txt"};
If your OS is UNIX based then change it accordingly.
Have you tried relative path for finding the location
like
abc (folder)
-> code(folder)
-->Program.Java
-> temp
--> file1.txt
so
when u run ur program in Eclipse IDE
ur relative path will be from program.java file is
../temp/file1.txt
And try to use / instead of \ so that it wont take as an escape character.
when u run from a jar
You need to extract the temp folder from jar to outside
abc (folder)
-> jar (folder)
-->Program.jar
-> temp
--> file1.txt
OR
Read the jar content from the program as a zip file. Reach your temp folder inside it by code and then read the content as input stream.
Related
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);
If I run my Java program in NetBeans and follow the information given in the output window to run from a command line:
To run this application from the command line without Ant, try:
java -jar "C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\assignment2.jar"
The program starts to run, but when it comes to run the following code to open a .txt file (my "database"):
System.out.println("Loading database of stored transactions...");
try
{
file = new File("TransactionDetails.txt");
inFile = new Scanner(file);
}
// if the log couldn't be found in the default program location
catch (FileNotFoundException ex)
{
System.out.println(CustomMessages.FileNotFound() +
System.getProperty("user.dir")); // display default directory
System.out.println(CustomMessages.systemExit());
System.exit(1); // the program needs the log to function as intended
}
It cannot find the .txt file and prints the default directory as the Windows System32 folder. How can I specify the location to be the Project folder as expected?
You could use an absolute path to the file instead of a relative path. e.g
file = new File("C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\TransactionDetails.txt");
inFile = new Scanner(file);
You cannot rely on the current working directory to be set to anything.
Either provide the file as a class path resource instead or ask the jvm where the class is located in the file system and locate the file relative to that.
For a read only file I would consider providing it as a resource.
I have mkdirs code like that;
File dir = new File ("/Mydir/");
if(dir.exists()==false) {
dir.mkdirs();
}
it is normal working and create directory on windows but not working on linux..
/MyDir/ is a reference to a directory inside root-dir / - you'll need root privileges to write there.
For creating a directory inside user home you could use "~/MyDir" in linux - but that will not work again in Windows.
If you're forced to use old-style File operations you could go
new File(new File(System.getProperty("user.home")), "MyDir").mkdir();
Better yet would be to invoke
Files.createDirectories(
Paths.get(System.getProperty("user.home"), "MyDir"));
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.
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()