20

I tried this code in my Netbeans 7.4 and it works without problem

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class JavaApplication148 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://redmine.ackee.cz/time_entries.xml");
        HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();
    }
}

However then I used it in eclipse in my maven-driven project and it throws following exception :

java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl 
cannot be cast to javax.net.ssl.HttpsURLConnection

This is following class in my maven project that throws an error

package cz.ackee.redmine.commands;

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public abstract class RedmineCommand {      
    public void write(String xmlAddress, String text, String requestMethod) throws IOException{         
         URL url = new URL("https://redmine.xxx.cz/time_entries.xml");
         HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); //this line throws exception
    }       
}

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cz.ackee</groupId>
    <artifactId>pan-unicorn-bot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Pan Unicorn Bot</name>
    <description>Pan unicorn IRC Bot</description>

  <repositories>
  <repository>
    <id>apache-snapshots</id>
    <url>http://repository.apache.org/snapshots/</url>
  </repository>
  </repositories>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.3-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.taskadapter</groupId>
            <artifactId>redmine-java-api</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>/src/main/assembly/binary.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>cz.ackee.unicorn.PanUnicornMain</mainClass>
                        </manifest>
                        <manifestEntries>
                            <mode>development</mode>
                            <url>${project.url}</url>
                            <key>value</key>
                        </manifestEntries>
                    </archive>
                </configuration>

            </plugin>
        </plugins>
    </build>

</project>

Any idea, why it does compile and run without problem on netbeans and why it does not go well on maven-eclipse project?

(I compile through command line using mvn package, I run it through eclipse)

2
  • I would guess that there's a difference in your classpath. Commented May 7, 2014 at 12:05
  • classpath to what exactly? Commented May 7, 2014 at 12:15

5 Answers 5

28

The solution is to change this line

URL url = new URL("https://redmine.xxx.cz/time_entries.xml");

into this line

URL url = new URL(null, "https://redmine.xxx.cz/time_entries.xml", new sun.net.www.protocol.https.Handler());
Sign up to request clarification or add additional context in comments.

2 Comments

I was getting the same exception in latest luna eclipse. Your solution worked for me.
Please do not use this solution. It's a workaround by using a specific handler implementation. This potentially prevents your code from running on a different JRE/JDK (vendor and/or version). More info here: community.oracle.com/thread/1535882 and community.oracle.com/thread/1150553
18

If you are using simply HTTP protocol (not HTTPS), instead of:

HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();

Use this:

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

Just drop the "s" in "HttpsURLConnection"

2 Comments

Upvoted because this is what I was looking for. But this only works if the protocol is http, for example: http://www.mywebsite.com (notice the http), and not in a secure protocol https (like the OP case).
HttpURLConnection is also valid for https url as I explain in my answer
3

To avoid this kind of error you can use HttpUrlConnection (java.net.HttpUrlConnection) for both http and https url

HttpsUrlConnection only add extra methods that "can be used" for https connection but you probably won't use these methods.

Both HttpsUrlConnection and HttpUrlConnection derives from UrlConnection which contains methods that permit to read the response like getInputStream(). HttpsUrlConnection derives from HttpUrlConnection which derived from UrlConnection.

java.net.HttpUrlConnection just add methods relative to http protocol (these methods apply to https)

https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/HttpsURLConnection.html

Thus you can do this :

URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

As well you can do this and obtain the same result, the connection will be the same :

URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

Comments

1

It called if url starts with "https"

    if(url.startswith("https"){ HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); }

use netty-3.9.2 > .jar to solve this issue When you use this netty.jar. the process of post call changed.

http://dl.bintray.com/netty/downloads/netty-3.9.12.Final.tar.bz2

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes
0
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    URL urlCopy = new URL(url);
    HttpsURLConnection httpconnect = (HttpsURLConnection)urlCopy.openConnection();
    httpconnect.setConnectTimeout(5000);
    httpconnect.connect();
    if(httpconnect.getResponseCode()<=400) {
        System.out.println(url+" - "+httpconnect.getResponseMessage() +" - "+ httpconnect.getResponseCode());
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.