Does anyone know if it's possible to convert a String/file into java source which can be compiled at run time using something like JavaCompiler. It looks like this is possible with Java 6, but I haven't seen anyone say that JavaCompiler is available in Android.
Basically my main goal is to turn a String or file text into source code in Android. Does anyone know how that can be done?
Thanks!
Android runs Dalvik not Java 6. JavaCompiler is not included in standard Dalvik distribution, so you cannot use it. Dalvik runtime is designed for embedded system as such it is less dynamic, compiling code on the fly is one of the things that it is not supposed to do.
Try what Hyangelo suggested, or Google for other scripting libraries. Clojure for example. ;)
You could do something advanced and setup a web service to compile source for you. This service would accept java source, compile it into a dalvik compiled class and return it as a binary.
This binary could then be added to a custom class loader as described here: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
Although you are not compiling on the phone, this would be compiled during runtime and, once inserted into your classloader, available for execution.
Technically possible, but not easy. If you look at Terminal IDE they package in all the tools to compile Android byte code from source on the device. You could take a similar approach by writing out the string to a file on disk, compiling it, and then use DexClassLoader to load the classes from the compiled JAR file or APK.
Related
Here is my situation.
I am using a custom programming language thats syntax is based on Java.
In order to compile this *.custom file I use the java classpath to locate the .jar that contains the .class files. This converts the .custom to .java from there I compile down to Java Byte Code and run on JVM. Now I am looking to use this custom language on Android. Is there a way that I can incorporate a .custom file into android and compile into .java and then to Dalvik Byte Code to run? Looking for any suggestions to run .custom on Android
Thanks
The Android dev tools are ultimately operating on Java .class files. If you can compile your code into something that will run on the JVM with the class-library restrictions of Android, you can compile the .class into a .dex. Keep in mind that you'll need to be referring to the Android API in your programs so the Android installer can link them to the Android runtime.
You can either compile to .class and then convert that to .dex using the dx tool. Or you can use DexMaker to go right to .dex from source.
I'm trying to find a way to convert a dll to a jar file. I have a .net application that communicates with a java application. The core entities are .net objects which I have to duplicate manually in java.
I've read about IKVM but it seems that it converts only jars to dlls and not the other way around.
Edit: If there is a tool that creates java classes from a dll it is also fine.
Thanks in advance
There isn't such a tool.
A dll is a natively compiled library. That means its been compiled down to machine code. Probably compiled by a C/C++/C# compiler.
A jar file is a zip file that contains '.class' files, which are files compiled down to 'java virtual machine code'. Probably compiled by a java/clojure/scala compiler.
These are two very different incompatible things.
It's not impossible to create such a tool that would do this translation, but it would definitely be an extremely difficult task, as it would entail translating from one machine code, to another, and would need to manage multiple issues like dependency solving, different type structure etc.
HOWEVER, I'm imagining that you want to do this because you want to use a DLL within some java code. That is somewhat possible, but is actually quite complicated. You'll need to use the JNI.
Take a look at this question as it might help you achieve what you want to do:
Calling C++ dll from Java
This is actually an easy task to perform. Converting .dll to .jar is as simple as using com4j and a couple of commands on the command line.
Download com4j.
Open command line and navigate to com4j directory in above step.
Execute below command.
java -jar tlbimp.jar -o outputFolder -p nameOfPackage "pathToFile"
Then jar the results with the following:
jar cf desiredJarName.jar folderYouWantJard
I am trying to open Motorola BLE API library for Android. I am not too familiar with Java so I am not sure if it is even possible.
When I try to open .class file from the library with notepad++ it contains something like:
Êþº¾ and black squares.
Is there a way to open them properly?
You can use the tool javap to disassemble .class files (= compiled Java code) but that won't tell you much.
Try to find the official documentation or the source (= .java files) by googling for the class name instead.
You will need to de-compiler to read the .class files. There are various available in the market, which are open source and free. Here is one
A .class file means that the library has already been compiled into Java's version of machine code, so you won't be able to view it in notepad. There are programs for decompiling class files back into source, but you need to make sure that a) you have the rights to do so, and b) that the particular decompiler supports the class file's particular version of Java.
For more information on decompilers, see this question:
How do I decompile Java class files?
If there source code (.java files) are provided , better use those. If not, then you have to use one of the de-compiler program. But even then if the code is obfuscated , even de-compiler would not be of any use. You have to use just the API documentation for any work.
Java is a compiled language where the source is compiled into machine readable bytecode. You have to disassemble/decompile the bytecode to get it into an even semi-readable form.
Also note that the license of the library very probably forbids you from disassembling it.
If you are really using to use the api in your application then here is the way:
See the documentation of the api that is provided.
Also import the classes in with the import statement. For eg put the classes in a new folder as "api" in current folder and the import statement would be as follows:
import api.*;
Then in the application logic make use of documentation on how to access the library.
Documentation here might help: https://developer.motorola.com/docs/bluetooth-low-energy-gatt-framework-api/
Is it possible to link a C++ library to a Java program statically, in a way that will make them into a single file ,just like linking 2 C++ libraries?
(I read that java programs can also be compiled to EXE).
Theoretically this should be possible to create one EXE that already includes the required JNI functions used by the JVM.
This EXE would have to load the Java part by starting a JVM instance in the same process (by loading jvm.dll and executing it as shown in question JNI Java in c++).
The Java-EXE-wrapper I know do not support something like this as they come with a pre-compiled EXE that gets the used JAR attached as resource. Therefore I assume you would have to build you own C/C++ executable and implement all the functionality you need.
When I use JNI I include the dll with JNI support into my jar file. Then access it by classpath. You will have single jar file.
It isn't possible unless you have access to a static version of the jvm.lib library. It is distributed as a dynamic-link library referring to jvm.dll. You can't do this.
Background
I am writing a program that will do some bulk renaming of members and functions in a directory of java source code to de-obfuscate the code based on a look-up table .csv file passed in to the program.
What this is for is the source code I have was written against a obfuscated jar. I have a de-obfuscated version of the jar that was run through a customized version RetroGaurd and I would like to parse the mapping file that was passed in to RetroGaurd to de-obfuscate the function calls my source code makes in to the jar. If I just compile my code and run it through RetroGaurd too when I decompile I loose all of my nice commenting and formatting (unless there is a option on RetroGaurd that I missed).
Problem
I found the Abstract Syntax Tree parser built in to Eclipse and it looks perfect for my uses, however I am not planning on writing my program as a plugin for Eclipse, this is going to be a stand alone jar that can be run on any machine.
My main concern is as I write my code I am getting a lot of dependencies on internal jars that Eclipse uses. I know that if I conform to the EPL for the library jars I will have no issues distributing it, but I am concerned about this project getting bigger and bigger as I write it as more and more jars from Eclipse's SDK are required.
Are there any other projects out there that would give me the ability to parse Java source code to do find and replace reliably like AST will allow me to, or is there a way to use RetroGaurd (or a program like it) to run the same de-obfuscation but keep my comments and functions the same without needing to run the de-obfuscated program though a de-compiler afterwards?
If you're worried about the need to run Eclipse GUI to execute your code, then you could consider running the plugin in headless mode. This would allow your plugin to be run from command line. See this SO thread.
You could also use any other open source java compiler. For e.g., openjdk's Java compiler. Please refer to the Resources section.
Hope it helps.