I'm trying to open files from within Java with something like this:
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile.txt");
Of course it all works fine and dandy in most cases.
HOWEVER!
When I have a file with the unicode character u3000, I cannot open it! Even if the file exists.
For example:
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");
I get an Exception, EVEN WHEN THE FILE EXISTS
[java] java.io.IOException: Failed to open file:/E:/_prog/test%E3%80%80.txt. Error message: The system cannot find the path specified.
Please help me i tried pretty much everything. This is driving me insane :/
Edit:
To give some more info:
I can easily create file with this name from Java.
It seems it has something to do with whitespace
I don't know if it applies to other characters; I didn't find any yet. But of course if there's 1 there could easily be 100.
I'm pretty sure I can't read from the file or write to it from Java, but I haven't tested that since it isn't my main concern.
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");
That doesn't compile. Clearly it isn't your real code.
[java] java.io.IOException: Failed to open
file:/E:/_prog/test%E3%80%80.txt
And there is proof. Clearly you passed a URL to new FileInputStream(). It doesn't take a URL string, it takes a file name.
Ok i think i actually found a kind of solution to my question and i post it here to help any people that may have similar problems.
This fix only works for Windows (XP and up i think) BUT i don't even know if this problem exist in other OS. And even if it does a similar fix should be possible.
I am using the following code to succesfully open a file with the character:
Process p = new ProcessBuilder("cmd", "/c start \"\" \"E:\_prog\test\u3000.txt\"").start();
Which opens the file 'E:_prog\testu3000.txt'
As far as i know \u3000 is ideographic space character. To test your code I created a file with name CompanyAlt+3000Address.
Note: when you press Alt+3000, windows will create an ideographic space character. Then I copied the file name, to my java program and it worked for me.
Desktop.getDesktop().open(new File("C:\\Users\\Chandru\\Desktop\\Companyâ••Address.txt"));
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.
here is the screenshothere is the screenshot error
please help me solve this error for e to deploy our system this week.
It cannot find the file specified. The error is right there, FileNotFoundException.
Things to look into to remedy this.
1) Is the file actually there
2) Does java have access to that folder or are their access restrictions on it
3) Did you type the path correctly, looks like you are using unescaped windows style \ slashes which java doesn't like (thought it should have thrown the error earlier
Try looking at some of those errors. For more information on the FileNotFoundException https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html?is-external=true
I am using this code
String path = getClass().getResource("Template.xls").getPath();
When I run it on my machine (windows), everything is good. I even did system.out.println on the get resource part and on the get path part and the results were:
file:/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
However I am getting the following error reports from some users
java.nio.file.InvalidPathException: Illegal char <:> at index 4:
file:\C:\Software%20Com\SoftwareCom.exe!\assets\Template.xls
Iam not sure whats happening or why would it work for some and not others
Any pointers?
To answer this question properly, it would be helpful to know what you want to do with the path information. To read the file, you don't need the path.
You could just call
getClass().getResourceAsStream("Template.xls")
If you really want to know the path, you should call
URL url = getClass().getResource("Template.xls");
Path dest = Paths.get(url.toURI());
This might cause problems as you seem to pack your java files in a windows executable. See Error in URL.getFile()
Edit for your comment:
As I wrote above, you don't need the path of the source to copy. You can use
getClass().getResourceAsStream("Template.xls")
to get the content of the file and write the content to whereever you want to write it. The reason for failing is that the file in your second example is contained within an executable file:
file:\C:\Software%20Com\SoftwareCom.exe
as can be seen from the path:
file:\C:\Software%20Com\SoftwareCom.exe!\assets\Template.xls
The exclamation mark indicates that the resource is within that file.
It works within Netbeans because there the resource is not packed in a jar, but rather is a separate file on the filesystem.
You should try to run the exe-version on your machine. It will most likely fail as well. If you want more information or help, please provide the complete code.
I faced this same issue and go around it by using the good ol' File API
URL url = MyClass.class.getClassLoader().getResource("myScript.sh");
Path scriptPath = new File(url.getPath()).toPath();
And it worked!
I am working on a programm which reads the data from a barcodescanner. To connect with the scanner, i use usb4java which works great.
After i finished my programm i wanted to test in on another computer.
The result is a LoaderException from usb4java "Native library not found in classpath: /org/usb4java/windows-x86_64/libusb-1.0.dll".
I have searched on google and stackoverflow and found different a lot of answers about MANIFEST.MF, getResource() and getResourceAsStream() but nothing helped. It never worked on the other computer.
So i looked in the class from usb4java which throws the exception line 277:
https://github.com/usb4java/usb4java/blob/master/src/main/java/org/usb4java/Loader.java
So i thought i just add following line to my program code:
System.out.println(Loader.class.getResource("/org/usb4java/windows-x86_64/libusb-1.0.dll"));
The result on my computer shows the directory is found:
jar:file:/C:/Users/somename/Desktop/scanner/BarcodeScanner.jar!/org/usb/windows-x86_64/libusb-1.0.dll
The result on the other computer is just:
null
This indicates the problem is not in the program code.
I searched a lot of hours but I am not able to find out the problem.
Here a screenshot of the exception:
Edit:
I forgot to mention the jar-files: here is a picture from my project in eclipse:
Edit2:
content of MANIFEST.MF from directory META-INF:
When running my project, I get this error.
Error:java.lang.IllegalArgumentException: Invalid resource path: C:\Android\Source Code and Samples\turbo-editor-master\turbo-editor-master\app\src\main\res
Any idea what is causing this?
I am using source code from the Turbo Editor app and have done no changes to the app prior to running it.
Try two things:
- Clean your project. This helps most of the times
- Go to the directory mentioned in the Exception. Make sure it actually exists.
I found the error. While looking at the error, follow the path that it is referring to. If there is no path like that, or a file missing, then create it and move the file to that path. Should fix the problem.
Illegal argument exception is throwed when a function gets wrong (it cannot reasonably deal with it) argument. This can be caused by, for example, missing function that was somewhere overrided. In your case - maybe this is issue with path? I'm not sure but if the path exists you can try changing it so it does not have "spaces" and capital letters. Please dump complete log with error.