1

I have this simple java code:

package com.androiddesktoptest.testtest;

public class AndroidDesktopTestMain
{
    public static void main(String[] args)
    {
        callFromAndroid();
    }

    public static void callFromAndroid()
    {
        System.out.println("DESKTOP CODE CALLED FROM ANDROID");
    }
}

And I want to call it from Android like this:

import com.androiddesktoptest.testtest.AndroidDesktopTestMain;

public void onCreate(Bundle savedInstanceState)
{
    //...
    AndroidDesktopTestMain.callFromAndroid();
}

and I get java.lang.NoClassDefFoundError

I am using Eclipse and my project AndroidDesktopTest has checked to export AndroidDesktopTest/src. Also, eclipse autocomplete my code in Android, so it see correcly my code in desktop project.

What I am doing wrong? I did something like this in my previous-previous-previous project, from this time, maybe ADT has chaged or something...

* UPDATE * entire error 07-20 14:02:56.452: E/AndroidRuntime(30233): java.lang.NoClassDefFoundError: com.androiddesktoptest.testtest.AndroidDesktopTestMain

5
  • stackoverflow.com/a/10046725/1289716 Commented Jul 20, 2012 at 12:30
  • I don't have problem with jars, but with separated project. Commented Jul 20, 2012 at 12:30
  • 07-20 14:02:56.452: E/AndroidRuntime(30233): java.lang.NoClassDefFoundError: com.androiddesktoptest.testtest.AndroidDesktopTestMain Commented Jul 20, 2012 at 12:31
  • How are the 2 projects linked? Commented Jul 20, 2012 at 12:46
  • Anders Metnik - one project export source, and second (Android) has java project in "required project in the build path". Is it enough? Commented Jul 20, 2012 at 12:57

4 Answers 4

3

Order & Export isn't the most well built feature of the Android ADT.

If you instead include the linked src folder from the Desktop project to your Android project (Right Click Project > Build Path > Link Source Folder) then the Class will be used as if it belongs to the Android Project.

I think the prefered way of doing this would be to create a jar file or a Library Project but these might be too cumbersome to try until you have stable Deskptop code.

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

2

Sounds like you don't get the class over to the phone when u run your android.

Make sure that:

right click project - properties - java build path - Order and Export (is it checked here?)

6 Comments

In order and export, move up your external project so it is almost at the top (right under your android project).
I think he is referring to the precedence of the library export order.
src is at the top from the beginnning
And after that needs to be your java project, else it won't reference it right when you load it onto the device.
Else attach a screenshot of it (the export order). And I can explain it out from that.
|
0

This article about including Java libraries into Android projects suggests replacing the "JRE System Library" with the android.jar for your API level in your linked Java project's build path dialog.

Comments

-1

try this

new AndroidDesktopTestMain.callFromAndroid();

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

3 Comments

What? public static void callFromAndroid()
I m not getting you.. Have you tried the above code? Is it working?
callFromAndroid is static, so I don't understand your answer... and yes, I tried it (look at another answer, same strange advice).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.