E.S.
E.S.

Reputation: 2841

Java/Android - Crash - Apache commons-io-2.4.jar

I am trying to save values in a string to a file on my local system by using the org.apache.commons.io.FileUtils static method writeStringToFile.

So, first I downloaded the commons-io-2.4.jar, along with its javadocs and source and imported it into my Eclipse project through the Java Build Path. Everything compiles just fine.

However, when I add the simple line:

org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File(Environment.getExternalStorageDirectory().toString() + "/logt.txt"), rslt.toString());

The program crashes. But, it doesn't crash anywhere near that statement. Instead, it crashes at the constructor of the Object which contains the method which calls this function.

In other words

To make it more eary, I instantiate the object within a Try, catch (RejectedExecutionException e) block but instead the program crashes at this part:

PathClassLoader.findClass(String) line: 243 **Last call in the stack**
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573   
PathClassLoader(ClassLoader).loadClass(String) line: 532    
Translate$4.run() line: 159  **Crash happens here where I instantiate a TranslateTask object**

So PathClassLoader... Not sure how to approach debugging this. All I know is that if I commend out the org.apache.commons.io.FileUtils.writeStringToFile() call, the error never happens and the code runs fine everywhere.

Running this on the API8/10 of Android, using Eclipse Indigo.

EDIT- Logcat:

08-07 18:40:11.409: I/System.out(1395): debugger has settled (1363)

08-07 18:40:14.399: W/KeyCharacterMap(1395): No keyboard for id 0

08-07 18:40:14.399: W/KeyCharacterMap(1395): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

Don't think it gives any hint, but this is all that logcat gives after the debugger has settled.

EDIT2 - For good measure, I just added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> But still no go

EDIT3 - I am starting to think the apache commons-io-2.4.jar doesn't work with Android inherently. Could that be it?

EDIT4 - In case anyone wants to take a crack at it, here is my code. It is made in Windows 7, Eclipse Indigo. The code is based on the "Hello Android" translate section. All I am trying to do is extract some JSON into a string and save it into my sdcard. But, it has evolved into this mystery here... https://dl.dropbox.com/u/10790286/Translate.zip (See TranslateTask.java line 111)

EDIT5 - Interesting update. When I manually put FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); into the eclipse expressions box during a debug, I get the following error

An exception occurred: java.lang.NoSuchMethodError

I don't understand why I would get this error because 1) the WriteStringToFile() function is in the commons-io source. 2) I can instantiate a FileUtils object 3) I imported it into my program.

Whats going on here?

Upvotes: 0

Views: 1998

Answers (2)

Frohnzie
Frohnzie

Reputation: 3569

Writing to the path "c:\\temp\\logt.txt" isn't going to work on Android. You can use something like Environment.getExternalStorageDirectory().toString() + "/log.txt" to write to the SD card.

EDIT: The follow worked on my Galaxy Nexus:

try {
  FileUtils.writeStringToFile(new File(Environment.getExternalStorageDirectory(),"log2.txt"), "HelloNow");
} catch (IOException e) {
  Log.e(TAG,e.getMessage())
}

Upvotes: 1

Anand Kumar
Anand Kumar

Reputation: 66

Though the programme runs succesfully in complier ,the programme crashes while it runs in android device .Because during the run time it checks over jar files within android dependancies only. I too had similar type of problem .I got it solved when in preferences/properties -> java build .Tick the external jar file .

Upvotes: 0

Related Questions