I am getting this error:
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Emre\Desktop\PN1g1z.gif
And I really don't get what's wrong.
This is what throws the exception:
Media media = new Media(file.getAbsolutePath());
Media expects an URI as String in the constructor. So, instead of using File#getAbsolutePath(), you should be using File#toURI() instead.
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#toURI%28%29
From the Media#new JavaDoc (thanks #Andreas):
source - The URI of the source media.
Actually, it's a big problem where you put your server.
I already faced this problem before. I used Geronimo with the space in my direction D:\Common DevTool\Geronimo.
You have two ways to resolve:
Change to D:\Tool\Geronimo. It ran well.
Your directory is not correct: C:/Program Files. You should move your server to another place without the space in the name.
Upgrade the JSF version.
I had a similar problem but another exception: java.lang.IllegalArgumentException: Illegal character in opaque part at index 2: C:\Users\MyUser\project\src\main\packagex\file.csv
The project was created using java not by me. It tried to read a csv file. It worked ok on linux but crashed on windows 10.
My solution was change the absolute path to relative and switch reverse slash to "normal" slash. The url finally worked with this:
src/main/packagex/file.csv
It seems colon and reverse slash triggered the exception. That is the reason exception indicates index 2 because colon is located in second position at url
Illegal character in opaque part at index 2: C:\Users\MyUser...
Reference:
https://background.sysfactory.online/index.php/2022/09/23/solucion-java-lang-illegalargumentexception-illegal-character-in-opaque-part-at-index-2-cusersmyuser/
Related
The format of the jar: URI scheme is simply defined to be jar:<url>!/[<entry>].
Is it possible to "nest" two such URIs together and refer to a file inside an archive inside an archive this way? Something like:
jar:jar:http://example.com/!/archive!/file
This should designate /file inside an archive jar:http://example.com/!/archive, that is /archive found in a file provided by http://example.com/. However, URL.openConnection throws an exception:
java.net.MalformedURLException: no !/ in spec
I also cannot replace ! with %21 since that makes the jar: URI invalid, and I am not aware of any escaping that could be performed. Is this somehow possible? Or, as a more generic question, is it possible to store a URI with !/ inside <url> so that it is preserved?
As it is stated in the source code java.net.JarURLConnection does not handle nested Jar URIs.
Your example jar:jar:http://example.com/!/archive!/fileis parsed as entry archive!/file in the archive jar:http://example.com/. The latter is not a valid jar URI. You can not either escape the !/ sequences, because the text after jar: and before !/ must be a valid URI without any translation.
You can of course provide your own URLConnection implementation that supports nested Jar URIs.
While the other answer is indeed correct in the case of JarURLConnection, the use of jar: and similar URIs is not restricted to it. Such is the case of the Apache Commons VFS:
jar:// arch-file-uri[! absolute-path]
Where arch-file-uri refers to a file of any supported type, including other zip files. Note: if you would like to use the ! as normal character it must be escaped using %21.
"Normal character" means usage of the character in path to the file inside the archive, not in the inner URI. This makes jar:jar:http://example.com/!/archive!/file valid, as it is the last occurence of ! that should be used as a delimiter.
Note that the inner URI should probably still remain unescaped, thus it is impossible to use # there.
I'm trying to upload an image file to a sqlite database via it's file-path using the code below
// Upload image to table //
byte[] bytesFromFile = null;
try {
String fileName = imageField.getText();
File f = new File(fileName);
bytesFromFile = Files.readAllBytes(f.toPath());
} catch (Exception e) {
e.printStackTrace();
}
stmt.setBytes (12, bytesFromFile);
When executed, I end up with this error
java.nio.file.InvalidPathException:
Illegal char <:> at index 2: ?C:\Users\SAMUE\Downloads\BigWindow.jpg
What I find very strange is I could have absolutely sworn when I tested this two days ago it worked fine, but maybe I'm just losing my mind.
I've tried implementing the other solutions for similar problems on here (converting to uri, etc.) but they are either not working or I just don't know how to implement them in the way I need to.
I don't know if this is useful information but for all the similar problems it seemed a leading '\' character was the culprit, but as you can see I'm getting that question-mark placed at the beginning of the file-path, I'm guessing that that may be a source of the issue, or that may just be an eclipse thing.
EDIT:
Hello, so sorry to waste your time, I have found the solution, it seems getting the path from the TextField appended that "?", I merely took the substring after it and it works fine now. I'm leaving this up in hopes that anyone that has a similar issue can be helped. If you think this is better off deleted, let me know.
In this link, it has been explained how to use the Twitter search API, for example:
Your search URL is:
https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
when I do a GET call using the link from POSTMAN (setting OAuth 1 params)it works fine, but when i do the same GET call from JAVA, it gives me the following error:
Caused by: java.net.URISyntaxException: Illegal character in query at index 57: https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
it works fine if i just search with one word without space and OR.
It sounds like you need to encode the URL properly. For instance a space character must be replaced with %20. Here is a table of encodings. Really, you should use a built in function. See this thread.
General Information
Windows 10 Home Edition , Java 8 Update 121
Description
I have an simple Java program that renames Files and Folders .
As I read from different articles Maximum filename length is generally 255 for modern versions on operating Systems , like Linux, Windows, Mac Os.
So I keep it maximum of 240 just in case.
Maximum filename length in NTFS (Windows XP and Windows Vista)?
https://serverfault.com/questions/9546/filename-length-limits-on-linux
https://apple.stackexchange.com/questions/86611/does-os-x-enforce-a-maximum-filename-length-or-character-restriction
https://support.microsoft.com/en-us/help/2891362/a-file-copy-operation-fails-when-files-or-folders-have-long-paths-in-windows-explorer
Test cases
So I have a Folder in the path C://GOXR3PLUS//..//Folder which contains a very simple sqlite3 database File named dbFile.fb .
Case 1 ❌
I rename the Folder to Folder plus 203 characters , so the folder name is Folderrrrrr.... until 207 characters. Trying to connect to the sqlite3 database I get this exception:
SEVERE:
java.sql.SQLException: The database has been closed
at org.sqlite.core.NativeDB.throwex(NativeDB.java:471)
at org.sqlite.core.NativeDB.errmsg_utf8(Native Method)
at org.sqlite.core.NativeDB.errmsg(NativeDB.java:137)
at org.sqlite.core.DB.newSQLException(DB.java:921)
at org.sqlite.core.DB.throwex(DB.java:886)
at org.sqlite.core.NativeDB._open_utf8(Native Method)
at org.sqlite.core.NativeDB._open(NativeDB.java:71)
at org.sqlite.core.DB.open(DB.java:174)
at org.sqlite.core.CoreConnection.open(CoreConnection.java:220)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:26)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at database.DbManager.<init>(DbManager.java:149)
at application.Main.lambda$8(Main.java:508)
at java.lang.Thread.run(Thread.java:745)
Case 2 ✅
I rename the Folder to Folder plus 196 characters so the folder name is Folderrrrrr.... until 201 characters. No exception occurs trying to open the sqlite3 database.
Finally
I am trying to open the dbFile.db with notepad for the first case , and it opens. Eclipse reports file not found 😆😆 , and with the Java application I am getting the error i posted .
Image from Eclipse error
My Question is:
Why does this happen, even though I am not even passing 210 characters for Folder Name?
There is a limit on path length, not only file name length. You are probably exceeding path length.
From Windows docs:
Maximum Path Length Limitation
In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH,
which is defined as 260 characters. A local path is structured in the
following order: drive letter, colon, backslash, name components
separated by backslashes, and a terminating null character. For
example, the maximum path on drive D is "D:\some 256-character path
string" where "" represents the invisible terminating null
character for the current system codepage. (The characters < > are
used here for visual clarity and cannot be part of a valid path
string.)
Note File I/O functions in the Windows API convert "/" to "\" as
part of converting the name to an NT-style name, except when using
the "\?\" prefix as detailed in the following sections.
The Windows API has many functions that also have Unicode versions to
permit an extended-length path for a maximum total path length of
32,767 characters. This type of path is composed of components
separated by backslashes, each up to the value returned in the
lpMaximumComponentLength parameter of the GetVolumeInformation
function (this value is commonly 255 characters). To specify an
extended-length path, use the "\?\" prefix. For example, "\?\D:\very
long path".
Note The maximum path of 32,767 characters is approximate, because
the "\?\" prefix may be expanded to a longer string by the system at
run time, and this expansion applies to the total length.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
Could you please help me find a solution for this problem?
I am trying to make a java GUI with Swing components
The interface is supposed to accept a Windows path to a certain file and trigger a set of function on the file after pressing the submit button
Right now I have created the component to get the user input as:
JTextField introducedPath1 = new JTextField(50);
I tried to change the default Windows path obtained by copy pasting the path from explorer into an accepted File path:
File file;
String makeCanonicalPath=introducedPath1.getText().toString();
makeCanonicalPath=makeCanonicalPath.replaceAll("\\", "/");
file = new File(makeCanonicalPath);
But I keep getting this error:
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
Which I assume it is caused by the default path in windows(ex:L:\practice\test) getting its '\' interpreted as escape sequences.
Any help or suggestion will be appreciated
With replaceAll, you need to use java escaping for regular expressions, so
makeCanonicalPath.replaceAll("\\\\", "/");
Because it is not a text search-replace, but a regex one.
If you want text-based search-replace, use apache commons string replace, for example.
Also, one alternative is to use replace() - http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replace(char, char) instead of replaceAll - it would replace all occurrences without using regex, and since you only have one char to replace, it'd work.