0

I'm having serious problems understanding how to manage folders vs. packages names when it comes to deploying Java. I have this little program, constructed like this:

deployer-classpath.bat
deployer-modulepath.bat
src
   data
      com
         myname
            appname
               data
                  *.java
                  resources.properties
                  resources_en_GB.properties
   main
      com
         myname
            appname
               main
                  App.java (main)

So, my intentions were to have two packages: com.myname.appname.data and com.myname.appname.main, and correct me if I am wrong, I choose this layout so to be able to create two modules to set up the app in a modular way.

I compile using deployer-classpath.bat (old-style compilation):

call "javac.exe" -d ".\output\classes" ".\src\data\com\micesp\PMapp\data\*.java"
call "jar.exe" --create --file ".\output\libs\data.jar" -C ".\output\classes" .

rmdir /q/s ".\output\classes"
mkdir ".\output\classes"

call "javac.exe" -d ".\output\classes" -classpath ".\output\libs\data.jar" ".\src\main\com\micesp\PMapp\main\*.java"
call "jar.exe" -c -f ".\output\libs\main.jar" -C ".\output\classes" .

rmdir /q/s ".\output\classes"

call "java.exe" -classpath "%cd%\output\libs\data.jar";"%cd%\output\libs\main.jar" "com.micesp.PMapp.main.App"
<!-- language: lang-none -->

Well, this is the output:

Exception in thread "main" java.lang.ExceptionInInitializerError
        at com.micesp.PMapp.main.App.main(App.java:25)
Caused by: java.util.MissingResourceException: Can''t find bundle for base name com.micesp.PMapp.data.resources, locale en_GB
        at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
        at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
        at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
        at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
        at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
        at com.micesp.PMapp.data.ProductManager$ResourceFormatter.<init>(ProductManager.java:219)
        at com.micesp.PMapp.data.ProductManager.<clinit>(ProductManager.java:36)
        ... 1 more

Same result if I run it as a modular application, using deployer_modulepath.bat:

call "javac.exe" -d ".\output\classes" ".\src\data\com\micesp\PMapp\data\*.java"
call "jar.exe" --create --file ".\output\libs\data.jar" -C ".\output\classes" .
rmdir /q/s ".\output\classes"
mkdir ".\output\classes"
call "javac.exe" -d ".\output\classes" -classpath ".\output\libs\data.jar" ".\src\main\com\micesp\PMapp\main\*.java"
call "jar.exe" -c -f ".\output\libs\main.jar" -C ".\output\classes" .
rmdir /q/s ".\output\classes"
call "java.exe" --module-path ".\output\libs" -m "main/com.micesp.PMapp.main.App"
mEm
  • 353
  • 3
  • 12
  • What does `call` do? Can we get just the command line input without a batch file in the way? – markspace Apr 22 '21 at 13:26
  • `call` just executes command in the `cmd` of Windows. – mEm Apr 22 '21 at 13:34
  • 1
    `call` is specifically for invoking other batch files. For an ordinary exe, there is no point in doing `call program arguments` instead of just `program arguments`. – Holger Apr 22 '21 at 14:30
  • 1
    To run a module, you have to specify `-m "main/com.micesp.PMapp.main.App"`, i.e. separate the module name with a *slash* from the main class name, not a backslash. Of course, this won’t change the fact that when you don’t add the resource(s) to the jar, you’ll get a `MissingResourceException`. Unlike some IDEs, `javac` won’t copy resource files to the target directory. – Holger Apr 22 '21 at 14:43
  • @Holger call works either with other batch as well as simple `.exe`.However, that's not the point of the question. Either with or without the `call` keyword, I get same result. Coming to your second answer, so how to add resource(s) to the `.jar` file? I mean, from what I understood, `.classes` compiled files should be placed into the `.jar` file when calling `jar.exe` utility, isn't that? – mEm Apr 22 '21 at 14:49
  • 2
    *Everything* needed at runtime should be in the .jar file. You only added the `.class` files, but not the `.properties` files. – Holger Apr 22 '21 at 14:51
  • @Holger May I ask you how to do that? I suppose it is not the same as compiling and adding a `.class` file into the `.jar`. – mEm Apr 22 '21 at 14:55
  • 2
    You may copy the properties files to the classes directory before you create the jar file or you create the jar file containing the classes first, followed by updating the jar file (using `u` instead of `c`), adding the properties files directly from the source directory. I suppose, you want a command to automatically find and handle all resource files instead of specifying each resource manually, but that’s a specific Windows batch file question, which I can’t answer. – Holger Apr 22 '21 at 15:02
  • @Holger, your answers solved the problem. If you wish, you can assemble an answer that I can vote up. Thanks. – mEm Apr 22 '21 at 15:22

1 Answers1

0

Without being able to try this myself it is hard to diagnose the problem. But are you sure the JARs contain what you think they contain and in the right folders? Your manual scripting of the compilation and packaging is a likely source of errors. Doing that sort of thing manually with Java requires a lot of things to be exactly correct. That is why build tools like ant, maven and so on are usually used and also the reason people use IDEs. You pick a tool, layout your source to match the tools preferences and then take advantage of the tool's abilities to abstract away all the complexity of the compilation and packaging.

I would suggest you perhaps take a look at Maven as a good starting point and rework your folder structure to play nicely with that.

JohnXF
  • 972
  • 9
  • 22