Springboot

Good afternoon,
there is a clear way to include snap java api in a springboot project?
I try many configuration with idea with and without springboot, without success.
I have tried some instructions here https://github.com/senbox-org/s2tbx.
Anyone can help me?
Kind regards
Michele Spinella

UPDATE: I have configured the enviroment as per https://github.com/senbox-org/s2tbx and now it works properly, but not on a springboot context in a clear maven project.
I can run snap desktop and I can read some S2B products in BEAM format but non in safe format:

File file = new File("/home/corvallis/dev/processor/S2B_MSIL1C_20210509T094029_N0300_R036_T33TXF_20210509T104807.dim");
prod = ProductIO.readProduct(file);     <- this works properly and then I can read the data in the model


File file = new File("/home/corvallis/dev/processor/S2B_MSIL1C_20210509T094029_N0300_R036_T33TXF_20210509T104807.SAFE");
prod = ProductIO.readProduct(file); <- this raise a nullpointer exception

my conf
adoptopenjdk 1.8.0
OS: debian 10
I have imported the modules, I run my code simply by a Java Application, may be I have to add some in the run configuration?
Thanks
Kind regards
Michele Spinella

Probably you don*t have all necessary dependency on your classpath.
For reading S2 data in Safe format you need for example the module s2tbx-s2msi-reader.

Maybe the the pages in our wiki can help you further.
SNAP development - SNAP - SNAP Wiki (atlassian.net)

ProductIO.readProduct(file) raises a NPE is strange. Can you post the stacktrace?
Can you test what happens when you change the file pointer to
“/home/corvallis/dev/processor/S2B_MSIL1C_20210509T094029_N0300_R036_T33TXF_20210509T104807.SAFE/MTD_MSIL1C.xml”
But actually it should work with the folder.

Maybe the OpenJpeg library has not been initialized.
Maybe you need to call: org.esa.snap.runtime.Engine.start()
in your initialization code
This will ensure that the engine is started and all modules/libraries are initialized.

Good day,
this is the stacktrace, including in the classpath the s2tbx.
Kind regards
Michele

Connected to the target VM, address: '127.0.0.1:48935', transport: 'socket'
Exception in thread "main" java.lang.NoClassDefFoundError: org/esa/s2tbx/dataio/VirtualDirEx
	at org.esa.s2tbx.dataio.s2.filepatterns.S2NamingConventionUtils.transformToSentinel2VirtualPath(S2NamingConventionUtils.java:306)
	at org.esa.s2tbx.dataio.s2.l1b.S2L1BProductReaderPlugIn.getDecodeQualification(S2L1BProductReaderPlugIn.java:71)
	at org.esa.snap.core.dataio.ProductIO.getProductReaderForInput(ProductIO.java:301)
	at org.esa.snap.core.dataio.ProductIO.readProductImpl(ProductIO.java:252)
	at org.esa.snap.core.dataio.ProductIO.readProduct(ProductIO.java:204)
	at org.acme.snap.examples.ndvi_simple.NdviSimpleMain.run(NdviSimpleMain.java:79)
	at org.acme.snap.examples.ndvi_simple.NdviSimpleMain.main(NdviSimpleMain.java:64)
Caused by: java.lang.ClassNotFoundException: org.esa.s2tbx.dataio.VirtualDirEx
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 7 more
Disconnected from the target VM, address: '127.0.0.1:48935', transport: 'socket'

Process finished with exit code 1

This class is located in the module s2tbx-commons. So you need this.
But if you use maven you should automatically get this dependency included in your project, because the s2tbx-s2msi-reader module has a dependency to it.
I don’t know how springboot works. Maybe this needs some extra configuration.

In addition I would suggest to checkout the snap and s2tbx code locally.
How to build SNAP from sources - SNAP - SNAP Wiki (atlassian.net)
This way you can find out quickly in which module missing classes are located.

Good day,
now it works, I can properly read sentinel-2 safe product. To test, I have used snap-engine-examples, in the pom.xml of NdviSimpleMain.java I have added:

<dependency>
    <groupId>org.esa.s2tbx</groupId>
    <artifactId>s2tbx</artifactId>
    <version>9.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.esa.s2tbx</groupId>
    <artifactId>s2tbx-s2msi-reader</artifactId>
    <version>9.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.esa.s2tbx</groupId>
    <artifactId>s2tbx-commons</artifactId>
    <version>9.0.0-SNAPSHOT</version>
</dependency>

using this reposistory

<repository>
    <id>snap-repo-public</id>
    <name>Public Maven Repository for SNAP</name>
    <url>http://nexus.senbox.net/nexus/content/repositories/public/</url>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>warn</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>true</enabled>
        <checksumPolicy>warn</checksumPolicy>
    </snapshots>
</repository>

Now I wan to switch in a clear springboot project. I prefer to use maven instead of local jars, in your opinion is a correct way?
Thanks a lot for help
Michele

1 Like

Yes, maven is always a good idea or Gradle or similar. Somthing which takes care of the dependencies.
This saves you the hassle with the jars.