0

I'm looking to write a Java program which will download a Java source file in text format off the web, compile it, load it, and use it as part of the running program. I've heard this is possible, but don't know how to write the code to make it happen. A fully functioning example or tutorial would be great, if you could point me in the direction of documentation such as this.

Once I learn how it's done, I plan to use this knowledge to build an Android Application which can customize itself with code from the web.

Asher Walther
  • 1,327
  • 2
  • 9
  • 13
  • *"I'm looking to write a Java program which.."* For deploying on Android? It would be tricky with J2SE, and that has the `JavaComplier` available in JDKs. – Andrew Thompson Jul 05 '12 at 23:09
  • Hi Andrew. Yes, I plan to use this knowledge to build an Android application. – Asher Walther Jul 05 '12 at 23:13
  • It might be easier to embed an existing scripting language -- Lua? -- or design your own scripting language specific to your application, and extend your application with _that_, instead. Compiling Java to bytecode to running it all on a memory- and processor- constrained device seems like pushing the boundaries. – sarnold Jul 05 '12 at 23:13
  • 3
    Hmmm, self-modifying code. Let me know what your app will be called so I can avoid it. – Squonk Jul 05 '12 at 23:14
  • 1
    Surely there is no reason to compile on the machine. That's the whole point of bytecode. Why not remove the compiler from the equation and pre-compile before downloading. – Joe Jul 05 '12 at 23:16
  • Hi Squonk. Don't worry; I'm not building a Trojan Horse. Trust me! :-) – Asher Walther Jul 05 '12 at 23:18
  • Hi Joe. That's a very good idea. If there are examples showing how it's done, I would love to see them. Thank you! – Asher Walther Jul 05 '12 at 23:19
  • @AsherWalther : I don't care what your intentions are, I simply don't want any app running on any of my Android devices which hasn't been developed properly and tested fully. There's a very easy way to have an Android app 'modify' itself - the user sets 'Allow automatic updates' and the developer modifies, tests and publishes updated apks. Alternatively, the user disables automatic updates and has freedom of choice if they want to use something new/updated from that which they installed in the first place. – Squonk Jul 05 '12 at 23:25
  • 1
    one word *uninstall* if I ever come across such an app! – t0mm13b Jul 05 '12 at 23:28
  • Am seeing more and more OP questions like these that are abusing the open-source nature of Android, earlier on someone asked how to control and redirect the flow of an application from a service... you want a platform to abuse .... there's Microsoft Windows for example.... just saying – t0mm13b Jul 05 '12 at 23:41

4 Answers4

1

A desktop program could use a shell script to download, compile and run a Java program. Android does not have a compiler, and adding one is non-trivial. The easiest way would be to also make a server program. The Android program would then tell the server program to download and compile the Java source code, and then send the result to the Android program, which would then load it using its ClassLoader.

One caveat is that the JDK compiler produces bytecode for the standard Java Virtual Machine, whereas Android's JVM is uses the Dalvik VM, so when you compile the Java class, you can't just use the JDK; you have to use the Android SDK to produce compatible bytecode that the Android ClassLoader can use.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
0

Yes, it's possible in the general sense to do what you want. In the specific case of Android, however, it is likely that the sandbox imposes restrictions that will make what you're trying to do difficult or impossible. To do this in the general case you can use an approach like:

  1. Use a web library of your choosing (for example, HttpClient, or HtmlUnit, or for simple tasks, Java's built-in URLConnection class is entirely acceptable as well) to download the Java file locally.

  2. Use System.exec() to fork a javac process to compile the downloaded Java file for you (or use a JavaCompiler implementation to do the same). Note that this might be a bit tricky if the downloaded Java file uses external JAR's/libraries that aren't on your system.

  3. Use the ClassLoader to load your compiled class. Note that you'll only be able to use it if your runtime classpath also includes any external JAR's/libraries that the code you're loading in relies upon.

However, step #2 will almost certainly not be possible on an Android device. Instead you'd need to compile Android-compatible class files somewhere else (like on a server, as Yusuf suggests), and then download and load the compiled class files from your app. Or, if you're really looking for a challenge, perhaps you could package a full Java compiler in your app, and compile the Java file(s) that way.

Community
  • 1
  • 1
aroth
  • 54,026
  • 20
  • 135
  • 176
0

What you are trying to do is something similar to either the JRuby-for-Android project or AIDE both of which builds on device, but only in the case of creating apps to be run on Android as opposed to adding code functionality to an currently running app. While JRuby-for-Android is more for scripting, it may provide enough functionality as it is open source and you may be able to modify it to fit your needs.

The AIDE project appears to be more of an achievement as you can write in Java on device to build an app. Features include Dropbox and git support. AIDE appears to be a closed-source app.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
-1

Permissions will be a problem. You can't write files to the partition where the app is installed. Maybe you can if you move the app to the SD card.

An alternative is to create a custom classloader and use that to feed class files to the runtime. I don't know if you can do that on Android.

Is scripting an option? I'm sure you can find a scripting interpreter for Android. Then you can download and execute the script in the interpreter.

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
  • It's an option, so long as the script has full access to the Android API. – Asher Walther Jul 05 '12 at 23:49
  • Typically the interpreter gives you control over exposing what you want to expose to the script. – Sarel Botha Jul 05 '12 at 23:54
  • Apps are not installed on /system. You can't write to where they are installed either, but you don't need to. If you want to install a new app, you point the platform to where you put it temporarily and it pops up a dialog for user confirmation before installing it in runnable form. If you want to use code in your app, you use a class loader with the location in your private storage where you are keeping it. – Chris Stratton Jul 05 '12 at 23:57