Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.MalformedURLException: no protocol

Tags:

java

url

jaxb

xjc

I' m writing a class to run xjc in java. my code goes as follows:

 URL url = new URL("C:\\Users\\Simran\\Desktop\\books.xsd"); 
 SchemaCompiler sc = XJC.createSchemaCompiler();
 sc.parseSchema(new InputSource(url.toExternalForm()));
 S2JJAXBModel model = sc.bind();
 JCodeModel cm = model.generateCode(null, null);
 cm.build(new FileCodeWriter(new File("C:\\Users\\Simran\\Desktop\\books.xsd")));

however I get the following error when I run this:

Exception in thread "main" java.net.MalformedURLException: no protocol: books.xsd
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at jaxbTest.Test1.main(Test1.java:22)

Can anyone help with this please?

like image 405
simran Avatar asked Dec 29 '10 18:12

simran


People also ask

How do I fix Java net MalformedURLException no protocol?

The only Solution for this is to make sure that the url you have passed is legal, with a proper protocol. The best way to do it is validating the URL before you proceed with your program. For validation you can use regular expression or other libraries that provide url validators.

What is Java net MalformedURLException?

Class MalformedURLExceptionThrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.


2 Answers

Try append "file://" to the beginning of your file path. But as Bozho proposed, you don't need an URL here.

like image 152
Vladimir Ivanov Avatar answered Sep 21 '22 13:09

Vladimir Ivanov


This is not a valid URL. It can be made valid by prepending file:// as protocol.

But you don't need a URL at all. You can pass a Reader (as well as an InputStream) to the InputSource constructor. So for example:

new InputSource(new FileReader(path))
like image 43
Bozho Avatar answered Sep 20 '22 13:09

Bozho