1

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>

Macklin
  • 167
  • 1
  • 11
  • *since the project uses Maven*... Did you make Maven explicitly move all `src/view/*.html` files into the classpath of the application? In other words, `src/` doesn't exist after the code is compiled and ran – OneCricketeer Sep 10 '17 at 14:45
  • Yes, I tried adding it as a resource under in the pom.xml, but it didn't change anything. It's especially confusing since it works in Eclipse, w/o any changes to the pom. – Macklin Sep 10 '17 at 14:52
  • Can you inspect the `build/` or `target/` folder that IntelliJ creates and see what's in it? – OneCricketeer Sep 10 '17 at 14:55
  • 1
    Interesting. There's target/classes/TweetView.html and target/classes/view/TweetView.java. The TweetView.html resource is no longer in the view folder, but moved up on level... – Macklin Sep 10 '17 at 14:59
  • 1
    Precisely the problem. Ideally, you have `src/main/java` and `src/main/resources`. Otherwise, you'll need to modify the POM – OneCricketeer Sep 10 '17 at 15:02
  • 1
    Okay, I see, thanks for the answer. I don't mind changing the POM or, if need be, the project structure. Still, I'm going to see if anyone can explain why the problem doesn't exist with Eclipse, just out of curiosity =). – Macklin Sep 10 '17 at 15:11
  • Also, marking things as "sources" in Intellij doesn't affect Maven. You should rely solely on the resources marked by the POM, then let the IDE pick up those properties – OneCricketeer Sep 10 '17 at 15:16
  • Yes, that makes sense. I actually didn't mark it as a source folder myself, the IDE did this automatically. I just wanted to point out what the structure looked like. Sorry, that was a bit unclear. – Macklin Sep 10 '17 at 15:22
  • Can you add your POM to the question? I'm curious why `src/view` is picked up at all – OneCricketeer Sep 10 '17 at 15:24
  • Okay, I added the POM. `src/view` is only picked up if I explicitly add it as a resource, that should be the reason. If I remove it, `target/classes/TweetView.html` is no longer there. – Macklin Sep 10 '17 at 15:32

1 Answers1

1

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245