Antonio
Antonio

Reputation: 71

Running android java code as standard java application

I have an Android application that has a core module. It's easy to code the module running it as standard CLI Java.

But if I copy this code in an Android Eclipse project the bytecode produced is for the dalvik VM, and it's not compatible with JRE.

How to easy develop in this scenario...

It's possible to launch in some way code in an android project as standard JRE project? It's possible to code this code in a separate standard project and include a jar file or as dependency in the android project?

[edit]

I have tried the solution from Alécio, I have build two project:

JVM project with this code:

package jvm;
public class SimpleJvm {
    public SimpleJvm() {
        System.out.println( "Ciao!") ;
    }
    public static void main(String[] args) {
        SimpleJvm simple = new SimpleJvm() ;
    }
}

packed it into a jar file a running gives:

C:\Users\Antonio\Desktop>java -cp simplejvm.jar jvm.SimpleJvm
Ciao!

but using it in an Android Project with this code (jar copied in the libs dir):

package com.simpleandroid ;

import android.os.Bundle ;
import android.app.Activity ;
import android.view.Menu ;
import jvm.SimpleJvm;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState) ;
        setContentView( R.layout.activity_main) ;

        SimpleJvm simple = new SimpleJvm() ;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate( R.menu.main, menu) ;
        return true ;
    }

}

gives:

07-05 16:51:33.386: E/dalvikvm(2102): Could not find class 'jvm.SimpleJvm', referenced from method com.simpleandroid.MainActivity.onCreate
...
07-05 16:51:33.686: E/AndroidRuntime(2102): FATAL EXCEPTION: main
07-05 16:51:33.686: E/AndroidRuntime(2102): java.lang.NoClassDefFoundError: jvm.SimpleJvm
07-05 16:51:33.686: E/AndroidRuntime(2102):     at com.simpleandroid.MainActivity.onCreate(MainActivity.java:15)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.Activity.performCreate(Activity.java:5104)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.os.Looper.loop(Looper.java:137)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 16:51:33.686: E/AndroidRuntime(2102):     at dalvik.system.NativeStart.main(Native Method)

I think the cause is the different .class file format... It's possible in eclipse in some way to compile a jar using the dalvik vm and a jar with the jvm?

Upvotes: 2

Views: 1373

Answers (2)

Antonio
Antonio

Reputation: 71

Ok so I think that the solution is to use ant or maven or similar to build two different jar files one compatible with jvm and one compatible with dalvik.

A start point can be: http://code.google.com/p/maven-android-plugin/wiki/GettingStarted

I don't know how to specifically make this but I think it's possible in some way.

Upvotes: 1

Alécio Carvalho
Alécio Carvalho

Reputation: 13667

You can create a normal extra Java project for it and you can add it as dependency on Android. In other words, you can use build tools, like Ant, Maven to generate the jar for this project and you can attach this jar into the /libs folder, then it will be automatically incorporated into your Android project. I'm assuming you're using Eclipse IDE.

Upvotes: 0

Related Questions