damonkohler / sl4a

SL4A brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device.
Apache License 2.0
2.42k stars 804 forks source link

Importing java class files within beanshell #214

Open damonkohler opened 9 years ago

damonkohler commented 9 years ago

From @GoogleCodeExporter on May 31, 2015 11:27

What device(s) are you experiencing the problem on?
HTC Legend

What firmware version are you running on the device?
2.1-update1

What steps will reproduce the problem?
1. I put 2 java6 classes (MyStaticClass.class and MyDynamicClass.class) in 
/sdcard/myjavaclasses

2. MyStaticClass.doIt() prints "do it"
MyDynamicClass.doIt() returns "do it" String

3. I executed the useMyClasses.bsh script, this is the code:

addClassPath( "/sdcard/myjavaclasses" );

try {
    MyStaticClass.doIt();
} catch( e ){
    print( e + "\n" );
}

try {
    MyDynamicClass p = new MyDynamicClass();
    print( p.doIt() );
} catch( e ){
    print( e + "\n" );
}

What is the expected output? What do you see instead?
I have the following error. Evaluation error: Sourced file: [...] unknown 
error: can't load this type of class file : at line 3 : in file [...]

What version of the product are you using? On what operating system?
sl4a_r2.apk beanshell_for_android_r1.apk on Android 2.1

Please provide any additional information below.

Original issue reported on code.google.com by andrea.i...@gmail.com on 9 Sep 2010 at 3:04

Copied from original issue: damonkohler/android-scripting#423

damonkohler commented 9 years ago

From @GoogleCodeExporter on May 31, 2015 11:27

You probably need to convert your files to dex format first.

See: 
http://davanum.wordpress.com/2007/12/04/command-line-java-on-dalvikvm/
for examples on how to use dx and aapt.

Original comment by rjmatthews62 on 19 Jan 2011 at 1:05

damonkohler commented 9 years ago

From @GoogleCodeExporter on May 31, 2015 11:27

[deleted comment]
damonkohler commented 9 years ago

From @GoogleCodeExporter on May 31, 2015 11:27

This is how it works:

1. On the PC, dexify your standard Java jar file with the dx tool from the 
Android SDK
2. Copy the dexified jar to your Android device
3. Use following code to use the Java classes:

//***********
//bsh
import dalvik.system.PathClassLoader;
ClassLoader cl = new 
PathClassLoader("/mnt/sdcard/sl4a/scripts/javalibs/NanoXML-Lite-2-2-3ta1-3_droid
.jar",ClassLoader.getSystemClassLoader ());
this.interpreter.setClassLoader(cl);
nanoxml.XMLElement x = new nanoxml.XMLElement();
print ("Class instance created");
//************ 

Original comment by tom.ar...@gmail.com on 13 Apr 2011 at 6:32