2

In my gradle java project, if I run ./gradlew run -PappArgs="['file.dat']" it is compiled and my app succesfully uses the file located in /src/main/resources/. I have integrated Eclipse with gradle and imported my project into Eclipse. When I run the app in Eclipse with filname specified in the Run Configurations argument list for the project, it fails to find the file.dat with this code:

import java.io.InputStream;
import java.util.Scanner;
public class MyClass {
    public static void main(String[] args) {
        final String fileName = args[0];
        Scanner scanner = null;
        try {
            InputStream f = MyClass.class.getResourceAsStream(fileName);
            scanner = new Scanner(f, "UTF-8"); // fails here saying f is null
        } finally {
            if (scanner != null) scanner.close();
        }
    }
}

Project tree is classic: myproj/src/main/java/MyClass.java, myproj/src/main/resources/file.dat and myproj/src/test/java/MyTest.java.

Stephan Rozinsky
  • 553
  • 2
  • 6
  • 21
  • are you passing the parameter to gradle in the eclipse launch configuration? or directly in the java launch configuration if you are not running using gradle? – guido Mar 27 '15 at 23:11
  • I'm passing the parameter in Run Configurations of my project in Eclipse, where I tried several ways `/src/main/resources/file.dat`, `file.dat`, `./src/main/resources/file.dat`. Nothing works. In gradle I passed my parameter `file.dat` in the command line. – Stephan Rozinsky Mar 27 '15 at 23:14
  • is your `Class` in a package? (root of the classpath will be `src/main/resources`; so if you want to load using `MyClass.class.getResource` and MyClass is in package `com.example`, your file has to go in `src/main/resources/com/example`. Or better use `InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName)` – guido Mar 27 '15 at 23:16
  • I'm in `static main`, so can not use `this`. There are no packages. All my classes are in the `/src/main/java/` folder and using the default package. – Stephan Rozinsky Mar 27 '15 at 23:23

1 Answers1

1

Both a set back and a blessing from Eclipse: all of your resources (files and pictures included) must be in the Build Path of your project. Ensure that this is indeed the case. If it is still the case, consider not using your method of finding the file to read from: a good old fashion

Scanner in=new Scanner(new File("src/main/resources/file.dat"));
will usually work.
I hope this helps, and best of luck to you.
Zesty Memes
  • 442
  • 2
  • 11
  • Then there was no point of importing the gradle project into eclipse and using the convention that all files go to the resources folder which must be available for the environment. – Stephan Rozinsky Mar 27 '15 at 23:26
  • I ran into this and felt to elaborate on this. It is true that Eclipse is stubborn and always needs its own build file. However, the gradle eclipse command will generate your eclipse .classpath files for you. You only need to import the project as Gradle project, or refresh them if you have already done so. – Rens Groenveld Apr 19 '17 at 06:01