获取 java 异常:java.net.MalformedURLException:无协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/59832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:46:31  来源:igfitidea点击:

getting java exception: java.net.MalformedURLException: no protocol

javaurlio

提问by Craig H

I am currently calling the following line of code:

我目前正在调用以下代码行:

java.net.URL connection_url = new java.net.URL("http://<ip address>:<port>/path");

and I get the exception above when it executes. Any ideas as to why this is happening?

我在执行时得到了上面的异常。关于为什么会发生这种情况的任何想法?

采纳答案by matt b

Your code works perfectly fine for me:

你的代码对我来说非常好:

public static void main(String[] args) {
    try {
        java.net.URL connection_url = new java.net.URL("http://:/path");
        System.out.println("Instantiated new URL: " + connection_url);
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

Instantiated new URL: http://:/path

实例化的新 URL:http://:/path

Sure you have the right line of code?

确定你有正确的代码行吗?

回答by jjnguy

As a side note, you should be using URIbecause Java URL class is screwed up. (The equals method I believe)

作为旁注,您应该使用URI,因为 Java URL 类被搞砸了。(我相信equals方法)

回答by sblundy

That url string looks like it's invalid. Sure it's not supposed to be 'http://path'? Or are the server & port blank?

该 url 字符串看起来无效。确定它不应该是“ http://path”?或者服务器和端口是空的?

回答by Pavan

I have also had the same exception, but in my case the URL which I was trying to execute had a space appended. After removing the space it worked fine for me. Check that the URL does not have any trailing spaces in your case.

我也有同样的例外,但在我的情况下,我试图执行的 URL 附加了一个空格。删除空间后,它对我来说效果很好。检查 URL 是否在您的案例中没有任何尾随空格。

回答by Jyoti

I had the same error and it got resolved by the below :

我有同样的错误,它通过以下解决:

The jar files (JFree) which I added few days back got corrupted automatically and was causing this error. I downloaded the same files again from net and it worked fine for me.

我几天前添加的 jar 文件 (JFree) 自动损坏并导致此错误。我再次从网上下载了相同的文件,对我来说效果很好。