Skip to content

URLConnection.getContentType() returns null for html resource #6394

@AlexanderScherbatiy

Description

@AlexanderScherbatiy

Describe the issue

URLConnection.getContentType() returns null content type for html resource. Running the same sample with JDK returns text/html content type.

The result is the same for building a native image from a class file or from a jar file:
URLConnectionSample.java code:

import java.net.URL;
import java.net.URLConnection;

public class URLConnectionSample {

    public static void main(String[] args) throws Exception {

        String path = "/index.html";
        URL url = URLConnectionSample.class.getResource(path);
        URLConnection connection = url.openConnection();
        String type = connection.getContentType();

        System.out.printf("connection: %s%n", connection);
        System.out.printf("connection type: %s%n", type);
    }
}
// java + class
> java URLConnectionSample 
connection: sun.net.www.protocol.file.FileURLConnection:file:/home/samples/url/index.html
connection type: text/html

// native image + class
> native-image -H:IncludeResources=\\Qindex.html\\E URLConnectionSample
./urlconnectionsample 
connection: com.oracle.svm.core.jdk.resources.ResourceURLConnection:resource:/index.html
connection type: null

// java + jar
> jar cf url-connection-sample.jar URLConnectionSample.class index.html
> java -cp url-connection-sample.jar URLConnectionSample 
connection: sun.net.www.protocol.jar.JarURLConnection:jar:file:/home/samples/url/url-connection-sample.jar!/index.html
connection type: text/html

// native image + jar
> native-image -cp url-connection-sample.jar -H:IncludeResources=\\Qindex.html\\E URLConnectionSample
/urlconnectionsample 
connection: com.oracle.svm.core.jdk.resources.ResourceURLConnection:resource:/index.html
connection type: null

Steps to reproduce the issue

  1. Create index.html resource file:
<html>
<ul>
    <li>List Item One</li>
    <li>List Item Two</li>
</ul>
</html>
  1. Create and compile the URLConnectionSample.java
    javac URLConnectionSample.java
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionSample {

    public static void main(String[] args) throws Exception {

        String path = "/index.html";
        URL url = URLConnectionSample.class.getResource(path);
        URLConnection connection = url.openConnection();
        String type = connection.getContentType();

        System.out.printf("connection: %s%n", connection);
        System.out.printf("connection type: %s%n", type);
    }
}
  1. Run the URLConnectionSample with java:
java URLConnectionSample
connection: sun.net.www.protocol.file.FileURLConnection:file:/home/samples/url/index.html
connection type: text/html

The connection type is text/html

  1. Create the native image providing index.html as a resourse:
    native-image -H:IncludeResources=\\Qindex.html\\E URLConnectionSample

  2. Run the native image:

./urlconnectionsample 
connection: com.oracle.svm.core.jdk.resources.ResourceURLConnection:resource:/index.html
connection type: null

The connection type is null

Describe GraalVM and your environment:

  • GraalVM version: the latest snapshot graalvm-community-java17-linux-amd64-dev.tar.gz from GraalVM CE 23.1.0-dev-20230408_0311
  • JDK: openjdk version "17.0.7" 2023-04-18
  • OS: Ubuntu 20.04.2 LTS
  • Architecture: x86_64

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions