we are running jar using batch file as window service and place xml files in c:/processed
directory;
if xml has an errorneous format then move it to error folder; we are using below method to move the file to error folder but getting below exception.
I think when the process is running its not able to move the file to error folder .
FileUtils.moveToDirectory("a.xml", "c:/processed/error", false);
exception in movedErrorFolder: Failed to delete original file 'c:/processed/a.xml' after copy to 'c:/processed/error/a.xml'
The java process does not have the right to delete the file, but if you can write on it then you can delete it.
The file is locked by another process. This occurs typically when you write a csv open with Excel.
The file is locked by the java process self, close() the file before moving it.
After reader.close() put Thread.sleep() - It works like charm
reader.close();
// Thread is Explicitly made to Sleep as Threads were shared and files were not getting Moved.
Thread.sleep(10000);
Another exception giving more details is probably nested in the Exception, held by the cause property
1) Make sure you do not have the xml open in an editor/viewer or used by another process.
2) make sure you .close() before moving the file.
Check if you opened a file inside the directory for read or write and you didn't call .close(); before trying to delete the parent directory.
Could you try this method from apache:
void org.apache.commons.io.FileUtils.moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
e.g.
use this import
import org.apache.commons.io.FileUtils;
and your code would be like this:
FileUtils.moveFileToDirectory(new File("c:/processed/a.xml"), new File("c:/processed/error"), false);
I was struggling with this error since last 24 hours. None of the answers above worked for me. My operating system is Windows 7 64 bit and I am using JDK 6. I tried methods FileUtils.moveToDirectory as well as file.delete.
I suspected it had something to do with Java. I uninstalled and reinstalled JDK 6 (I ran the installer as an Administrator just to be sure) and restarted my machine and Eureka, the error disappeared and things started working.
Now don't ask me why I am still using JDK 6 in 2017 (Some enterprise software ****)
Related
so I did run into one very weird issue. The idea is simple: create temp dir, place some files in it and then try to access them. Now the problem is that calling File.createTempDir() or Files.createTempDirectory(prefix) creates new file inside AppData/Local/temp with shortened path, so the full path to folder looks something like C:/Users/FirstNam~1/AppData/Local/Temp/myFolder/myFile.txt instead of C:/Users/FirstName LastName/AppData/Local/Temp/myFolder.myFile.txt.
The difference is that generated path inside java contains FirstNam~1 instead of FistName SecondName. Java then throws exception File Not Found.
When I try to copy and paste shortened path into file explorer I get an error saying that file does not exist, but if I do change shortened path to full one then file opens and it works as intended.
Is there any way to fix it? Ether by forcing java to use full path names or enabling something in windows? I did Enable NTFS long paths policy, but it did not help.
This is happening when using java 8/11 and windows 10 running on VM, project is using AGP and gradle. Temp file is created inside groovy file that extends Plugin<Project>
Just when I lose hope and create a ticket, couple hours after that I find the answer. So, java has method Path.toRealPath() which solves this ~1 issue. After using this method paths no longer contain shortening and are correctly resolved.
EDIT: looks like java is doing everything correct and paths are actually valid, problem did come from library that I'm using and it's a bug with it.
So I have this code that works fine, it launch the .jar file from another machine that I have configure in my pc as a red ubication
Runtime.getRuntime().exec("java -jar Z:\\AAA\\BBB\\CCC\\ZZZ.jar");
But now I want to launch the .jar from that external path without using the shortcut before (so I can launch it with this code in a machine that dont have that red ubication configured)
Runtime.getRuntime().exec("java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
But doent work (I can access and open the file manually without a problem).
When I enter the java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar in the Command prompt it return me Error: Unable to access jarfile MMM\NNN, so perhaps one problem is that the path have a space in the folder name, but I think that may be something else too.
The question is, if the problem is the space, how I can solve it? I cant find a way. And in the other hand, how I can run it in another machine? I have to use that red ubication IP in some way instead?
PD: Using this code, it return me true
File f = new File("\\\\MMM\\NNN LLL\\OOO\\ZZZ.jar");
System.out.println(f.exists()); //--> true
So looks like the spaces dont interfere in the path (the four "\" doesnt seem to do anything in the tests when launching)
I have heard other people having such problems. The main reason for that is that probably Java exec method is not network (SMB) aware. So it doesn't even try to open from the network.
Anyway running the code like that from the network might not be the best solution. First of all the network might be unavailable, or the java file coming might be corrupted. If you want to do it properly you have several options:
Simple option that can work:
Create a bat file that works and exec that one - you can even copy the file locally first to make sure it is available first (if it is big and the network fails)
A better solution :
Use java to copy the file to the working directory and execute it from there.
A plus to do it like that by downloading is that you can maintain a version of the file (hash?) so you don't download it if it is the same. Or you can have fallback - execute the last downloaded version if the network drive is unavailable.
Use a local maven repository and dependency for that jar :)
This way it will keep the version of the jar locally and won't have to download it every time. It will also download a new version if available and the code will be more mainstream (for example not platform / pc dependent)
The answer give by #MadProgrammer works fine!
ProcessBuilder builder = new ProcessBuilder("java", "-jar", "MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
Lot of thanks! In any case going to check the ideas posted by Veselin Davidov
How can I check that I can delete a file in Java?
For example, I should be able to delete file C:/file.txt but I never will be able to delete the C:/ or Computer, or My Documents etc.
Solution described in possible duplicate does not work for me.
Removing file requires write permission of the file's parent, i.e. directory where file is stored. Directory in java is also represented by instance of class java.io.File that has method canWrite().
So, to check whether file can be deleted you should call file.getParent().canWrite().
On my Windows 7 64 bit box using NTFS and Java 7 (Oracle JDK), the only thing which worked for me reliably is
boolean canDelete = file.renameTo(file)
This is surprisingly simple and works also for folders, which have "somewhere below" an "open" or "locked" file.
Other things I tried and produced false-positives: aquire a FileLock, File#canWrite, File#setLastModified ("touch"), file.getParent().canWrite()
see i have following code in one native call
errno = 0;
FILE *fp;
fp = fopen("jigar.txt","wb");
if(fp == NULL)
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "FOPEN FAIL with %d",errno);
else
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "FOPEN pass ");
which gets fail and shows
FOPEN FAIL with 30
now here 30 means it shows error
#define EROFS 30 /* Read-only file system */
In MainFest file on my application i have added this line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
still i am getting this error..
How to resolve this issue?
Edit
thanks by specifies folder name it works But
In my case i have one Library which has such fopen() call where file names i can not give. It takes it default filename.
And i am using this Library in my ndk application so in this case how to solve this?
By default when you are running your App, it runs in its own context. fopen("jigar.txt","wb"); will try to open the file in current directory, which i think mostly would be /data/.. So you cant create files like that. Instead, if you want a folder inside /data/ you can call a function gteDir() and inside it you can create your own files. Ok this is all for general information.
Coming to your problem, as mentioned above you need to give absolute path to create a file in a different directory. This is the same case even in Linux.
For the Library thing, you can do two things.
1) Make changes in the library source code and compile it again using NDK. While making changes, give some string as the argument for fopen() which you will pass it while executing the Application.
2) remount the filesystem in which your library is creating the file, then do chmod 777 to the specific directory inside which your file is being created. Now execute the Application. It should work. But this is not generic. If you are doing it for some testing purpose then this solution is simplest and time saving...
If stuck somewhere, let me know..
You should specify folder where to write the "jigar.txt" file. Like this:
fp = fopen("/sdcard/figar.txt", "wb");
yuppi finally i got that
i have did this way
first change the current directory of my process from "/" to "/sdcard/"
chdir("/sdcard/");
and then it find path with respect to /sdcard/
and everything WORKING..!!!
I had problems while finding the path of file(s) in Netbeans..
Problem is already solved (checked answer).
Today I noticed another problem: When project is finished,
I have to execute the generated .jar to launch the program, but it doesn't work because an error occurs: NullPointer (where to load a file) when accessing/openning jar outside Netbeans.
Is it possible to open a file with the class file in Java/Netbeans which works in Netbeans and even in any directory?
I've found already some threads about my problem in site but none was helpful.
Code:
File file = new File(URLDecoder.decode(this.getClass().getResource("file.xml").getFile(), "UTF-8"));
The problem you have is that File only refer to files on the filesystem, not files in jars.
If you want a more generic locator, use a URL which is what getResource provides. However, usually you don't need to know the location of the file, you just need its contents, in which case you can use getResourceAsInputStream()
This all assumes your class path is configured correctly.
Yes, you should be able to load a file anywhere on your file system that the java process has access to. You just need to have the path explicitly set in your getResource call.
For example:
File file = new File(URLDecoder.decode(this.getClass().getResource("C:\\foo\\bar\\file.xml").getFile(), "UTF-8"));