Run java jar on android

How to run Java programs directly on Android (without creating an APK)

A step by step instruction for compiling a Java program into an Android executable and using ADB to run it.

When you want to create a system / commandline tool for Android, you have to write it in C(++)… or do you? TLDR; here’s the final proof of concept. Sticking with Java would have the benefit of avoiding all of the native ABI hassle and also being able to call into the Android runtime. So how do we do that?

A (not so) simple Hello World program

Let’s start with the Java program we want to run. In order to make it a bit more interesting (and because any useful program has dependencies), it won’t just print the obligatory “Hello World” message, but also use the Apache Commons CLI library to parse its commandline arguments:

package com.example; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class HelloWorld  public static void main(String[] args) throws ParseException  Option version = new Option("v", "Print version"); Option help = new Option("h", "Print help"); Options options = new Options(); options.addOption(help); options.addOption(version); for (Option opt : new DefaultParser().parse(options, args).getOptions())  if (opt.equals(version))  String os = System.getProperty("os.arch"); System.out.println("Hello World (" + os + ") v0.1"); > if (opt.equals(help))  new HelpFormatter().printHelp("Hello World", options); > > > >

Setting up the working directory

We will have to manually run several commandline tools in the next step, assuming the following final directory structure:

. ├── android-sdk-linux │ └── build-tools │ └── 23.0.2 │ └── dx ├── bin │ └── com │ └── example │ └── HelloWorld.class ├── lib │ └── commons-cli-1.3.1.jar ├── src │ └── com │ └── example │ └── HelloWorld.java ├── helloworld.jar └── helloworld.sh
  • Android SDK (either via Android Studio or the SDK Manager). NOTE: If you are an Android developer, you’ll have the Android SDK already installed. In that case, you don’t actually need to copy it to the working directory as long as you know the path to the dx tool.
  • Apache Commons CLI library v1.3.1

Afterwards copy&paste the HelloWorld code from above into the source folder. You might also find my semantic version parser class useful later on (not required here, though).

Compiling and dexing the Java class

Next step is to compile the java class (keep in mind that Android is stuck with Java 7 — bytecode for later versions won’t work). In case you are not used to doing this outside of an IDE, here’s the command:

javac -source 1.7 -target 1.7 -d bin -cp lib/commons-cli-1.3.1.jar src/com/example/HelloWorld.java

Make sure the program compiled properly:

java -cp lib/commons-cli-1.3.1.jar:bin com.example.HelloWorld -h usage: Hello world -h Print help -v Print version

Android cannot run Java class files directly. They have to be converted to Dalvik’s DEX format first (yes, even if you are using ART):

./android-sdk-linux/build-tools/23.0.2/dx --output=helloworld.jar --dex ./bin lib/commons-cli-1.3.1.jar

NOTE: Android Build Tools v28.0.2 and later contain a dx upgrade, called d8 . The d8 tool can process Java 8 class files. I’ll stick with dx for backwards compatibility reasons here.

Creating the startup shellscript

Android does not have a (normal) JRE, so JAR files cannot be started the same way as on a PC. You need a shellscript wrapper to do this. Copy&paste the one below to the workspace.

base=/data/local/tmp/helloworld export CLASSPATH=$base/helloworld.jar export ANDROID_DATA=$base mkdir -p $base/dalvik-cache exec app_process $base com.example.HelloWorld "$@"

NOTE: DEX files can also be started directly using the dalvikvm command, but going through app_process gives us a pre-warmed VM from the Zygote process (it is also the method employed by framework commands like pm and am ).

Installing and running the (non-) app

Time to push everything to the device:

adb shell mkdir -p /data/local/tmp/helloworld adb push helloworld.jar /data/local/tmp/helloworld adb push helloworld.sh /data/local/tmp/helloworld adb shell chmod 777 /data/local/tmp/helloworld/helloworld.sh

Moment of truth (fingers crossed):

adb shell /data/local/tmp/helloworld/helloworld.sh -v Hello World (armv7l) v0.1

NOTE: Since nothing was installed into the system, getting rid of the program is simply done by deleting the directory again.

It works, but how do I get a Context?!

Contexts represent an environment that is associated with an app (which we explicitly did not build) and are also device dependant. They can only be created by the ActivityThread class (a hidden system class that you cannot instantiate). If you want to interact with the Android runtime, you have to talk to the system services directly through their Binder interfaces. But that’s a topic for another article.

Follow up articles

Источник

Is there an app that runs Java (.jar) files on Android?

I would like to run a .jar file on my Samsung Galaxy S5 Android phone. I expect that it requires an app, but I can’t find such app anywhere. What app would be great to emulate .jar files, preferably without a .jar size limit?

3 Answers 3

Even though Android uses Java, it does not support executing JAR files on Android devices. However, you can use emulators to do this which are not available on Google Play Store but are available from the developers’ website. I have provided the source link and download instructions are available there.

Features of phoneME:

  • phoneME for Windows CE/Mobile and Android is an implementation of the phoneME open source J2ME application platform for your Windows Mobile phone or Android handheld device.
  • Each build includes the phoneME VM and an Android frontend wrapper with JNI bindings to the VM.
  • Most powerful, smooth user experience, less crashes.
  • JAD files are also associated with the Foundation Profile-MIDP Android application. Therefore, you can also use your browser to select an online JAD file to download and run the midlet. If you click on a JAD download link, a popup menu will appear and you need to choose the Foundation Profile-MIDP Android application
  • No root access required.

Features of Netmite App Runner:

  • This is the most popular application to run Java apps and games (J2ME/MIDP – jar/jad) app on Android OS.
  • This application has App Explorer, which helps to find existing Java apps and games on your phone/tablet device.
  • It is integrated with Browser-Browse any j2me site, click to run any jad/jar file.
  • It can auto-convert J2ME into Android package (apk) on the fly.
  • You can immediately port your existing Java apps (jar/jad) to Android WITHOUT source code.

Features JBED:

  • JBED is also a very good Java/J2ME Emulator for Android.
  • It is fast but has some issue like – screen got hazy on landscape mode, unexpected force close.
  • It has built in app explorer.

Features JBlend:

  • JBlend is very Similar to JBED and much better.
  • It’s smooth and never crashes.
  • Support most Java apps / games

Important! It’s possible that not all emulators will work for your devices. Each emulator has different features. You have the option to try different emulators, and decide which one you want to use.

Источник

How can I install .jar files on my Android?

I downloaded some 3D games, unzipped them and got .jar files. How can I use these in Android? I have a Galaxy S running 2.1.

5 Answers 5

Although Android uses java, it doesn’t support normal JAR files. Instead it uses an Android specific format called APK. The main difference is that Android does not use the normal Java Virtual Machine, but contains it’s own Dalvik Virtual Machine that’s optimized for Android.

@Jeevan Bhatt: Exactly. This can change in the future (if someone writes a JVM for Android) but I wouldn’t hold my breath.

I did search on internet and came to know that android support only apk file so i need to convert .jar into .apk by some converter, is it true?

Yes, there are some converters but these are not very reliable. The problem is, Android uses a part of the entire java library and adds it’s own libraries to control most of the features. For example, the Graphical User Interface classes can’t be used on Android (eg. swing). Also the converter needs to rewrite all UI events, such as touchscreen/keyboard functionality. Apparently some converters can do this for MIDP applets (J2ME) used on older phones, but normal Java apps (probably) will not work. You can try the converters, but most likely the result will lag, be buggy or crash.

I’d like to partially disagree with Ryan Conrad & Onik.

I have a Samsung Spica and it came pre-loaded with an app while it was running v1.6 and now running 2.1 (both official Samsung India versions) — called Java ME Apps.

I am running multiple Java apps (meant for non-Android phones) through it.

Both are JAR files and once run via the Java ME app, both run as expected.

So to sum up, there is a way to run JAR apps on Android. I do however, agree with everything else mentioned WRT APKs and Dalvik.

Android doesn’t actually even «use» java. The language that the code is written in is Java, but the android SDK takes the compiled bytecode and converts it in to the Dalvik bytecode.

So it is really a misconception that Android runs java. Android runs Dalvik, which the core libraries are based on the Java framework, but the end resulting binary files are not the same, and the virtual machines are also completely different.

Just like the Dalvik VM will not run Java bytecode, the Java VM will not run Dalvik bytecode.

Converting libraries to Dalvik is probably possible, if all the features are available in Android (or additional libraries), but anything that has a Java UI, will not work.

Источник

How to run Java app in Android

Is it possible to run java app (jar) in my android application? Because I need to create PDF, the problem is if I generate PDF in android, only can show with small image, if it contains large image in many pages, it will be error. So I think, I can generate PDF in java and then included to android app.

is it possible to run java app (jar) in my android application , no Android does not support Java. Sorry.

Java programs run everywhere where the JVM is installed. However android uses different VM — Dalvik. My guess would be you can do it only through emulator.

6 Answers 6

Concernig the mentiones app JBED: Well honestly, I could not find any credible source for this tools JBED, so I would really be very cautious (e.g. who is the developer?)

In the manifestfile (in Androidmanifest.xml, where every app has to state what rights it needs to run, see How to view AndroidManifest.xml from APK file?) there are many rights mentioned (what could be necessary, as the app wants to run as an emulator), so a java application might want to send an SMS, record audio, take pictures and place calls — so the emulator would need those rights as well.

But then the app also registers the «android.intent.action.BOOT_COMPLETED» event (i.e. autostart after boot) and this would go against every description of the tool.

Ah yeah and giveaway: The apk has a folder «certs» that has some (root-)certificates. But those are not the real certificates of the authorities, e.g. Versign. If one installs the app and by that those certificates the trust you might have in https-connections is lost because those who made the fake certificates can create own, false certificates that your phone would trust.

I assume (or am pretty sure) this is a spy tool, but I could be wrong. The (rare) testimonials that claim the tool ran perfectly will probably be the same person that posted the tool under a different name.

Источник

Читайте также:  Anchor Links
Оцените статью