java.net.MalformedURLException:没有协议

sim*_*ran 8 java url jaxb xjc

我正在编写一个在java中运行xjc的类.我的代码如下:

 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")));
Run Code Online (Sandbox Code Playgroud)

但是当我运行这个时,我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Vla*_*nov 17

尝试附加"file://"到文件路径的开头.但正如Bozho所建议的那样,你不需要这里的URL.


Boz*_*zho 10

这不是有效的URL.它可以通过预先file://作为协议来使其有效.

但是你根本不需要URL.您可以将Reader(以及一个InputStream)传递给InputSource构造函数.例如:

new InputSource(new FileReader(path))
Run Code Online (Sandbox Code Playgroud)