0

In module com.thing.withprops I have this code in com.thing.withprops.UseProps.java:

 URL url =UseProps.class.getResource("config/values.properties") ;

module-info is

module com.thing.withprops { exports com.thing.withprops;}

now there is another module with name com.thing.withprops.config where lies a values.properties file in directory com/thing/withprops/config/ the module-info is just this:

module com.thing.withprops.config{}

well when everything is jarred and executed all is working perfectly: the resource is found! I am confused because it seems that the doc says it shouldn't be found since it is in another module which is not exported or opened. So what is wrong? the way I understand doc (I am not a native speaker) or my code?

thanks for any hint

bear
  • 111
  • 4
  • 1
    Sounds like you have created a plain old jar instead of a [“modular JAR”](https://www.google.de/search?q=modular+JAR)… – Holger Mar 01 '18 at 15:16
  • no: these jars have their module-info.class at the top (I checked that immediately) – bear Mar 01 '18 at 15:27
  • 1
    and you ran the application with the jar on the module path instead of the class path? – Holger Mar 01 '18 at 15:50
  • just did "java -jar top.jar" (which is the jar with the main and manifest that describes Class-Path to the other 2 jars) ... is that the problem? then how to have a "java -jar" that behaves correctly? – bear Mar 01 '18 at 15:58
  • 2
    [`java [options] [--module-path modulepath] --module module[/mainclass] [args...]`](https://docs.oracle.com/javase/9/tools/java.htm#JSWOR624) – Holger Mar 01 '18 at 16:03
  • may be (but not sure since this is what the IntelliJ runs and it runs by delivering the URL ... I will test it from the command line) but how to do that with jars? – bear Mar 01 '18 at 16:09
  • yes the direct command with java fails ... but then how come the jar succeeds? – bear Mar 01 '18 at 16:12
  • 2
    `--module-path` resp. `-p` may point to the directory containing your modular jars. `-jar` simply exists for backward compatibility and doesn’t load the jar as a module. – Holger Mar 01 '18 at 16:33
  • Just to add to Holger's comment. `java -jar` is for executable JARs, the JAR file is added to the class path. So it's nothing to do with modules. – Alan Bateman Mar 02 '18 at 07:10
  • where is the fact that -jar is incompatible with modular jar documented? do you mean that if you deploy a jar to be clicked on (the old way) it's not supposed to work anymore? then what do you do to deploy jars simply (not talking of jlink or scripts)? – bear Mar 02 '18 at 08:01

1 Answers1

0

in fact there are two problems there : one is about handling resources and the other is about java -jar behavior

here is a suggestion on how to handle resources in a modular project:

public interface ResourceLoader {
public static Optional<URL> resourceSearch(Class clazz, String name) {
    String fullPath = name ;
    if(! name.startsWith("/")) {
        String packageName = clazz.getPackageName();
        fullPath = '/'+packageName.replace('.','/')+ '/' + name;
    }

    ServiceLoader<ResourceLoader> loader = ServiceLoader.load(ResourceLoader.class);
    for(ResourceLoader resourceLoader: loader) {
        Optional<URL>  anUrl =resourceLoader.getResource(fullPath);
        if(anUrl.isPresent()) {
            return anUrl ;
        }
    }
    return Optional.empty() ;
}

public static Optional<InputStream>  resourceSearchAsStream(Class clazz, String name) throws IOException {
    Optional<URL> anURL = resourceSearch(clazz, name) ;
    if(anURL.isPresent()){
        InputStream is = anURL.get().openStream() ;
        return Optional.of(is) ;
    }
     return Optional.empty() ;
}

public default Optional<URL> getResource(String fullPath) {
    Class localClass = this.getClass() ;
    URL res = localClass.getResource(fullPath) ;
    return Optional.ofNullable(res) ;
}

}

then one can deploy another module with resources with module-info like :

open module com.thing.withprops.config {
requires com.thing.withprops;
provides com.thing.withprops.utils.ResourceLoader with com.thing.withprops.spi.ResourceLoaderImpl ;

}

any other suggestion? (still trying to find a way to have something looking like a "clickable" jar ...but could be hopeless)

bear
  • 111
  • 4