Resource is loaded in Eclipse but not in IntelliJ

1.4k views Asked by At

I know, similar questions have been asked here, but none of them seemed to fit my exact problem. I have the following (partial) directory structure:

src
  |__view
       |__TweetView.java
       |__TweetView.html

In TweetView.java, I'm trying to get the resource URL of TweetView.html as follows:

String htmlDocName = "TweetView.html";
Class thisClass = this.getClass();
URL htmlResourceURL = thisClass.getResource(htmlDocName);

This works fine in Eclipse (Neon.2), but in IntelliJ (2017.2.3), htmlResourceURL is always null. I'm aware that the location of the HTML file might not be ideal (should go in a resources folder), but for various reasons, I am not supposed to change this right now.

I observed that the src-folder is marked as a "Sources" folder in the project structure in IntelliJ, and its sub-folders aren't explicitly marked. Project settings aren't shared across Eclipse and IntelliJ, since the project uses Maven. Note: The problem is stated with regard to running the project as a Java Application directly from within the respective IDEs, not to a JAR/WAR export.

I saw that IntelliJ has these "Resource patterns" which define what file types should be considered as resources, but I couldn't get it to work by adding .html, neither with nor without wildcards (and according to the IntelliJ website, .html is considered a resource by default anyway).

Does anyone have an idea what the problem might be? Since it works in Eclipse, I would ideally like to solve the problem through a configuration in IntelliJ, rather than changing the code.

EDIT: Here's my POM:

<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>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/view</directory>
        </resource>
    </resources>
</build>
<dependencies>
    <dependency>
        <groupId>com.twitter</groupId>
        <artifactId>hbc-twitter4j</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.0.M1</version>
    </dependency>
</dependencies>

1

There are 1 answers

0
OneCricketeer On BEST ANSWER

In Eclipse, if you just do "Run as Java Application", I don't think it actually executes Maven. Intellij, on the other hand will usually detect you have a Maven project and will run the necessary tasks to get the application running.

At least, that's my experience using both

There's target/classes/TweetView.html and target/classes/view/TweetView.java

You set the src directory for the sources (Java files) but the resources into the src/view folder.

So, all Java classes in the view folder are staying in the view folder. All files (except Java) in the view folder are moved to the root classpath, therefore the class loader doesn't know where to look from the given class

My suggestion would be to move back the Java files to Mavens preferred location of src/main/java, then you can leave the HTML files where they are