Java Question error: try-with-resources is not supported in -source 1.6 (INLINE JAVA)

DonManfred

Expert
Licensed User
Longtime User
I am trying to add a
B4X:
public static class StickerContentProvider extends ContentProvider
to the starterservice with InlineJava

The Class is using
B4X:
    private synchronized void readContentFile(@NonNull Context context) {
        try (InputStream contentsInputStream = context.getAssets().open(CONTENT_FILE_NAME)) {
            stickerPackList = ContentFileParser.parseStickerPacks(contentsInputStream);
        } catch (IOException | IllegalStateException e) {
            throw new RuntimeException(CONTENT_FILE_NAME + " file has some issues: " + e.getMessage(), e);
        }
    }
internally. This is the code-block which fails.
CONTENT_FILE_NAME is a filename for a file in the assets
B4X:
public static final String CONTENT_FILE_NAME = "contents.json";

B4A Version: 8.50
Parse den Code. (0.00s)
Building folders structure. (0.04s)
Kompiliere den Code. (0.01s)
Kompiliere Layoutcode. (0.00s)
Organisiere Libraries. (0.00s)
Generiere R Datei. (0.16s)
Kompiliere generierten Java Code. Error
javac 1.8.0_141
src\de\donmanfred\sticker\starter.java:320: error: try-with-resources is not supported in -source 1.6
try (InputStream contentsInputStream = context.getAssets().open(CONTENT_FILE_NAME)) {
^
(use -source 7 or higher to enable try-with-resources)
1 error

What do i need to rewrite the code to?
Can i enable -source 7 somehow?
 

DonManfred

Expert
Licensed User
Longtime User
I got it working with this code. Ok, not working. It can compile more. But i can not compile the hole app as of now. Need to fix more things ;-)

B4X:
    private synchronized void readContentFile(@NonNull Context context) {
        InputStream contentsInputStream = GetIS("AssetsDir","contents.json");
        stickerPackList = ContentFileParser.parseStickerPacks(contentsInputStream);
        //try (InputStream contentsInputStream = GetIS("AssetsDir","contents.json")) {
        //    stickerPackList = ContentFileParser.parseStickerPacks(contentsInputStream);
        //} catch (IOException | IllegalStateException e) {
        //    throw new RuntimeException(CONTENT_FILE_NAME + " file has some issues: " + e.getMessage(), e);
        //}

    }
    public InputStream GetIS(String Dir, String File) throws IOException {
     if (Dir.equals(anywheresoftware.b4a.objects.streams.File.getDirAssets())) {
       if (anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder != null) {
         return anywheresoftware.b4a.objects.streams.File.OpenInput(anywheresoftware.b4a.objects.streams.File.virtualAssetsFolder,
             anywheresoftware.b4a.objects.streams.File.getUnpackedVirtualAssetFile(File));
       }
       return anywheresoftware.b4a.objects.streams.File.OpenInput(BA.applicationContext.getAssets().openFd(File.toLowerCase(BA.cul)), 1);
     }
     else {
       return anywheresoftware.b4a.objects.streams.File.OpenInput(anywheresoftware.b4a.objects.streams.File.Combine(Dir, File), 1);
     }
   }

But now it cannot find another class which is used inside the ContentProvider. It is the
ContentFileParser

Can i add more than one Class to the Starter service using inline java?

I tried to add it after the ContentProvider class but it does not work. now that i think of i should have tried it with adding the Class before the contentProvider too. Will do test this evening.
 
Last edited:
Top