0

based on the Oracle documentation I tried to construct a URL and link it to a path on my HDD. I used the following codes to do so:

 String path = home+ File . separator + " dict ";
 URL url = new URL(" file ", null , path );

but then ,though on the Oracle documentation it is said that file can be one of the protocols at first argument, but it giving me the following exception in the practical:

Exception in thread "main" java.net.MalformedURLException: unknown protocol:  file 

why is that so?

0

3 Answers 3

1

You have extra space in your string: URL url = new URL("file", null , path );

Sign up to request clarification or add additional context in comments.

6 Comments

That's not the only problem, see my answer
@lonesome among other things such a code cannot work on Windows systems, since a backslash is illegal in a URI (and therefore URL) path component. Also, if you can use URI instead, it's much better.
@fge no no I wasnt asking you. I was asking tachyonflux that what it means by extra space?
I was just talking about " file " vs. "file"
@tachyonflux oh i got u now. i didnt thought it has such a impact. i men, i just simply using a tutorial and trusted its code. seems that was why it said unknown protocol though you should have made it more clear that where is the extra string heehee
|
1

You may be missing the protocol when you set the path for your html file. As you are trying to read a local file, you can use file protocol:

file:///{yourfilepath}

OR

You can get from a filename to a URI in Java using you can use,

new File(filename).toURI()

Hope this helps.

Comments

1

Don't use URL for that. And don't use File. This is 2014, we have java.nio.file.

Use this instead, it will give an immediate, correct result:

final URL url = Paths.get(home, "dict").toUri().toURL();

The scheme associated with the default filesystem is always file.

Also, why a URL and not a URI? And note that your code wouldn't work on Windows (you can't have a backslash in the path component of a URI).


edit: it also appears that the manual you use gets many things wrong; for one, it assumes a Unix system!

And one problem you have is that the WNHOME environment variable is not set on your system; as a result, you get a path which is your current directory, plus null, plus "dict".

Change your code to this:

final String wnhome = System.getenv("WNHOME");
if (wnhome == null)
    throw new IllegalStateException("WNHOME environment variable is not set");
final URL url = Paths.get(wnhome, dict).toUri().toURL();

Normally you will see an exception... It is up to you to set the WNHOME environment variable here.

9 Comments

sorry but I just used the manual from MIT for an API of Wordnet for Java . so I assumed they are perfectly considered everything!
Well, it appears that they have not ;) Seriously, though, a lot of tutorials on the net get file I/O either wrong or very wrong; learn to use java.nio.file, you'll never come back!
nice! but there is an issue with your code. I assigned the home string like this: C:\\Program Files (x86)\\WordNet\\2.1\\dict but with your code it looks like that the url is poiting at something like this: C:\\Users\\me\\Documents\\NetBeansProjects\\testWordnet\\null\\dict
Well, you don't tell what you want home to be in your question at all; I can't guess that for you ;) So, what is it supposed to be?
as i mentioned in the previous comment it should refer to that path which is the dictionary folder of Wordnet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.