Data in J2ME RecordStore does not persist across sessions

只愿长相守 提交于 2019-12-12 07:22:40

问题


I'm building a mobile app with J2ME, and I've found that the data I write into a RecordStore can be accessed while the program is still running but it is lost after quitting and restarting it. No exception is thrown, the data is simply lost.

UPDATE: Thanks everyone for your suggestions. I'm using NetBeans on Windows 7. I'm not sure if it is using the WTK version I have previously installed or another one it has installed somewhere else. I've checked my WTK folder for the files Pavel wrote about, but couldn't find them. Now I'm testing the features requiring persistence on my phone and everything else in the emulator, but it would of course be much better to be able to test everything in the emulator.

private RecordStore recordStore = null;

public MyMIDlet() {
    readStuff(); // output: nothing found in recordStore :(
    saveStuff();
    readStuff(); // output: stuff
}

private void readStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        String stuff;

        if (n == 0) {
            stuff = "nothing found in recordStore :(";
        }
        else {
            stuff = new String(recordStore.getRecord(1));
        }

        System.out.println(stuff);
    }
    catch (Exception e) {
        System.out.println("Exception occured in readStuff: " + e.getMessage());
    }
    finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            }
            catch (Exception e) {
                // ignore
            }
        }
    }
}

private void saveStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        byte[] stuff = "stuff".getBytes();
        recordStore.addRecord(stuff, 0, stuff.length);
    } catch (Exception e) {
        System.out.println("Exception occured in saveStuff: " + e.getMessage());
    } finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}

回答1:


If you use Sun WTK, it creates a file named "in.use" in its "appdb" folder:

C:\WTK25\appdb\DefaultColorPhone\in.use

If you close your emulator in unusual way (kill a process, for example), it would not delete it, and next time you run emulator, it would create temporary folder for storing data:

C:\WTK25\appdb\temp.DefaultColorPhone1

when starting this way, it should print in console: "Running with storage root temp.DefaultColorPhone1".

I fix it, including into my ".bat" file a line for deleting "in.use" file each time, emulator runs. But you should be careful when running several emulators at once.




回答2:


I experienced the same problem myself, I did however discover that NetBeans, or whatever, deletes the deployed program files after execution. These files are located in the C:\Documents and Settings\MyUser\javame-sdk\3.0\work\0\appdb folder, might be different on Vista/Win7 and I guess the number in the path refers to the emulator you are currently using. Anyways, in this folder look for something that is named like your RecordStore. E.g. "00000002_PSC_onfig.db", which is my suite configuration recordstore named PSConfig. By copying this to e.g. "Copy of 00000002_PSC_onfig.db" it will not be deleted. After NetBeans have cleaned up, just copy it back to its original name. The next time you hit run in NetBeans your recordstore will be there. It's pain, but at least it gives you the possibility to use the emulator to debug your RMS handling.




回答3:


This question has been around for a while but I stumbled upon it whilst looking for an answer to the same problem with the emulator but in my case it was when using the Java ME 3 SDK. It is possible that the solution I found might also fix this problem.

Using:

emulator -Xdescriptor:/path/to/app.jad

will according to the docs: "Install a MIDlet, run it, and uninstall it after it finishes."

To persist an installation (and it's data) you should use:

emulator -Xjam:install=<JAD-file-URL>

The JAD file URL can either be a web address or 'file:///path/to/app.jad' if you want to install from your local file system. This installation command will display an application storage number which you can then use to launch the emulator and run the previously installed app by calling:

emulator -Xjam:run=<application-storage-number>

See the docs for further command line options.




回答4:


I could finally get it to work on a real handset. It seems that, as Martin Clayton suggested, the emulator reset erased the data. I'm still looking for a way to enable persistence in the emulator though.




回答5:


If you are using windows Vista there can and almost are permission issues. I am not sure how to resolve this but you might want to check that the user that is running the emulator has access to write to the emulator store.

In appdb/$phone/*.db




回答6:


to fix the storage problem you need to check the option "Storage size" in the netbeans platform manager. Go in project properties; go in platform; manage emulators; select the sun java wireless toolkit; go in Tools and Extensions; open preferences; storage; storage size option... set a size.

this works for me




回答7:


I experienced the same issue on Ubuntu Linux and working with WTK 2.5.2 and Netbeans 8.0.2. I later figured it was caused by shutting down my laptop without closing the emulator. It also happens if you run a second emulator without shutting down the first.

My solution is based on the Best Answer here, just shut down all emulators and delete the file located at

~/j2mewtk/2.5.2/appdb/DefaultColorPhone



来源:https://stackoverflow.com/questions/1704509/data-in-j2me-recordstore-does-not-persist-across-sessions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!