java.net.MalformedURLException in Intellij IDEA [duplicate] - java

How come this code is giving me a unhandled exception java.net.malformedurlexception in java ?
String u = "http://webapi.com/demo.zip";
URL url = new URL(u);
Can someone tell me how to fix?

You need to handle the posible exception.
Try with this:
try {
String u = "http://webapi.com/demo.zip";
URL url = new URL(u);
} catch (MalformedURLException e) {
e.printStackTrace();
}

Use a try catch statement to handle exceptions:
String u = "http://webapi.com/demo.zip";
try {
URL url = new URL(u);
} catch (MalformedURLException e) {
//do whatever you want to do if you get the exception here
}

java.net.malformedurlexception
It means that no legal protocol could be found in a specification string or the string could not be parsed or your URL is not confirmed the spec or missing a component
I think this will help you to understand URL
https://url.spec.whatwg.org/

Related

How to handle exceptions with Jsoup in java to keep program running

my program crash when user inserts a weird url
It should go like
while(condition) {
try {
String url = reciveURL();
Document rss = Jsoup.connect( url ).get();
}
catch (MalformedURLException e) {
System.err.println("Invalid URL");
} catch (OthersExceptions e){
Others.Actions();
}
}
The problem is, this throws "java.net.MalformedURLException: no protocol", instead of printing "Invalid URL" and the program crash (when user inserts any other kind of text)
Thanks!
Lol, solved myself, but i'll let the question as theres no post on the same issue
You should import java.net.url, this brings the "URL" type, which triggers the MalformedURLException (Jsoup doesnt do this)
So it goes like this
while(true){
{
String url = reciveURL() ;
URL chk_url = new URL(url);
Document rss = Jsoup.connect( url ).get();
} catch (MalformedURLException e) {
System.err.println("url mal puesta!");
}
}

How to add wsdl file to java project resources correctly and get it path

i have wsdl file and i need to add this file to my java project resources. I have generated code from wsdl by wsimport.
What i have -
1) added file to resources package
2) in my class from wsipmort i change url property from
URL url = null;
url = new URL("http://someUrl/someWsdlFile?wsdl");
to
File file = new File("resources/someWsdlFile.wsdl");
String absolutePath = file.getAbsolutePath();
try {
url = new URL(absolutePath);
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
and when i run my code i get an exception -
javax.xml.ws.WebServiceException: java.net.MalformedURLException: unknown protocol: d
when i have url like this
url = new URL("file:C:/Users/someWsdlFile.wsdl");
my code works correctly without exception
unknown protocol: d
so what am I doing wrong?
To find a resource from the search path used to load classes, use following method.
ClassLoader.getSystemResource("someWsdlFile.wsdl");
If load your resource available in the classpath then use folloing method.
yourClass.class.getResource("someWsdlFile.wsdl");
Thank you Boris and ravthiru its work now.
i do this
String absolutePath = null;
try {
absolutePath = String.valueOf(ClassLoader.getSystemResource("someWsdlFile.wsdl").toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
URL url = null;
WebServiceException e = null;
try {
url = new URL(absolutePath);
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}

Open stream from uri

I got an uri (java.net.URI) such as http://www.example.com. How do I open it as a stream in Java?
Do I really have to use the URL class instead?
You will have to create a new URL object and then open stream on the URL instance. An example is below.
try {
URL url = uri.toURL(); //get URL from your uri object
InputStream stream = url.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
URLConnection connection = uri.toURL().openConnection()
Yes, you have to use the URL class in one way or the other.
You should use ContentResolver to obtain InputStream:
InputStream is = getContentResolver().openInputStream(uri);
Code is valid inside Activity object scope.
uri.toURL().openStream() or uri.toURL().openConnection().getInputStream()
You can use URLConnection to read data for given URL. - URLConnection

How to check if an URL doesn't exist?

I trying to check if an URL which I want to connect to exists or not. Here's my attempt:
try {
// Connect to the url
document = Jsoup.connect("http://www.malformedurl.com").get();
tags = document.select(".tags .tag a");
num = document.select(".tag .count");
// Take the wanted data
UrlFunctions.UrlParse(tags, num);
} catch (java.net.MalformedURLException e) {
System.out.println("URL DOESNT EXIST");
}
After running that, I don't get the message URL DOESNT EXIST. What exception should I use or what else should I do?
A MalFormedURLException will only be thrown when the URL is really malformed, i.e. it does not conform the URL spec, not when it does not exist. This is under the covers been thrown by the constructor of the java.net.URL class. Its javadoc tells the following:
throws
MalformedURLException - If the string specifies an unknown protocol.
So, it will only be thrown when you use for example "www.malformedurl.com" or
"foo://www.malformedurl.com" instead of "http://www.malformedurl.com".
To detect whether an URL exists you'd better to head for a different solution. If the host name is unknown, then you should catch UnknownHostException instead:
try {
document = Jsoup.connect("http://www.malformedurl.com").get();
// ...
} catch (UnknownHostException e) {
System.err.println("Unknown host");
e.printStackTrace(); // I'd rather (re)throw it though.
}
This is not necessarily a problem of the other end, it can also occur when the DNS server on your network is bogus.
Or, to detect whether an IP address is reachable, then you should catch SocketTimeoutException instead:
try {
document = Jsoup.connect("http://12.34.56.78").get();
// ...
} catch (SocketTimeoutException e) {
System.err.println("IP cannot be reached");
e.printStackTrace(); // I'd rather (re)throw it though.
}

can I check if a file exists at a URL?

can I check if a file exists at a URL?
This link is very good for C#, what about java. I serach but i did not find good solution.
It's quite similar in Java. You just need to evaluate the HTTP Response code:
final URL url = new URL("http://some.where/file.html");
url.openConnection().getResponseCode();
A more complete example can be found here.
Contributing a clean version that's easier to copy and paste.
try {
final URL url = new URL("http://your/url");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
int responseCode = huc.getResponseCode();
// Handle response code here...
} catch (UnknownHostException uhe) {
// Handle exceptions as necessary
} catch (FileNotFoundException fnfe) {
// Handle exceptions as necessary
} catch (Exception e) {
// Handle exceptions as necessary
}

Categories