getting java exception: java.net.MalformedURLExcep

2019-06-20 09:53发布

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?

标签: java url io
5条回答
Rolldiameter
2楼-- · 2019-06-20 10:23

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

查看更多
祖国的老花朵
3楼-- · 2019-06-20 10:27

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.

查看更多
forever°为你锁心
4楼-- · 2019-06-20 10:28

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

查看更多
你好瞎i
5楼-- · 2019-06-20 10:29

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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-06-20 10:34

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

Sure you have the right line of code?

查看更多
登录 后发表回答