Sunday, September 3, 2017

Compiling and running java applications from android terminal?



I recently got a bluetooth keyboard for my Motorola Xoom tablet, and upon realizing that BusyBox comes with vi for text editing I started wondering about writing and executing code on android.


I tried running $ javac in Terminal Emulator but to no avail; I got javac: not found


I looked around a bit and found a post saying that since android is run on Dalvik there is no way for the JVM to run, but it seems counter-intuitive that an operating system built on java wouldn't be able to compile and run a java program.



I managed to find an app called AIDE that allows one to write and execute android apps (in java), but that's still not quite what I'm looking for.


I want to be able to compile and run a standard java command line application on android. Does anybody know a way of doing this?



Answer



I found an app on the play store that let me answer this question. Below are the steps I used to get javac and java running.



Go to the play store and install Terminal IDE, this supports everything needed to write and run java applications: vim, javac, and java.


Once the app is installed, make sure you open the app and go to Install System to install everything needed.



Now press the Terminal IDE button to open the terminal.


Use vim to write and save your hello world program.



Then to execute it has to be compiled to a JAR file:


# javac hello.java
# dx --dex --output=hello.jar hello.class
# java -jar hello.jar hello


When I first tried to compile and execute I got


UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: hello.class: file not readable


This issue was fixed by compiling and running as superuser. Here's the full terminal output of my issue


terminal++@192.168.1.30:~$ javac hello.java
terminal++@192.168.1.30:~$ dx --dex --output=hello.jar hello.class

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: hello.class: file not readable
at com.android.dx.util.FileUtils.readFile(FileUtils.java:63)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:139)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:113)
at com.android.dx.command.dexer.Main.processOne(Main.java:247)

at com.android.dx.command.dexer.Main.processAllFiles(Main.java:183)
at com.android.dx.command.dexer.Main.run(Main.java:139)
at com.android.dx.command.dexer.Main.main(Main.java:120)
at com.android.dx.command.Main.main(Main.java:89)
at com.spartacusrex.spartacuside.external.dx.main(dx.java:14)
at dalvik.system.NativeStart.main(Native Method)
1 error; aborting
terminal++@192.168.1.30:~$ su
# dx --dex --output=hello.jar hello.class
# java -jar hello.jar hello

Hello world
#


Everything you could possibly want to know can be found in the Help section of the Terminal IDE app, especially in the Java sections of the Tutorial.


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...