termux / termux-packages

A package build system for Termux.
https://termux.dev
Other
13.29k stars 3.06k forks source link

Package request: javac/jar/jdk #324

Closed ghost closed 3 years ago

ghost commented 8 years ago

Please add the Java compilers (java, javac, jar, etc.)

neerajvashistha commented 8 years ago

Yeah it would be much appreciated

ghost commented 8 years ago

Ya, Terminal IDE is really hard to use. I've tried importing their binaries to a custom folder to run, but it doesn't work

TheDiamondYT1 commented 7 years ago

I can get the JRE 8 Runtime working on Android but it requires mounting the libraries whoch requires root

vishalbiswas commented 7 years ago

@TheDiamondYT1 please, elaborate. I've been trying to get openjdk to build for Android but it fails for shm support.

TheDiamondYT1 commented 7 years ago

@vishalbiswas Im not entirely sure if this is correct, because this is a new phone and i had it working on the old one, but here is my script.

NOTE!!!!! Please compile and use my app to download and run the script if you are on lollipop or below. Or just go to my app repo's "download" branch and get java.zip and copy to /sdcard/.JRE4Droid then run the script in the system somewhere

#!/system/bin/sh
# Java Installer by TheDiamondYT
#
# Set DIR variable
dir=$(dirname $0)
busybox=$dir/busybox
#
# Mount / read write
$busybox mount -o rw,remount /
#
# Create java directory
$busybox mkdir $dir/java
#
# Extract java.zip here
$busybox unzip /sdcard/.JRE4Droid/java.zip -d $dir/java
#
# Create directory for java libs
$busybox mkdir /lib
#
# Umount
$busybox umount /lib
#
# Bind folders
$busybox mount -o bind $dir/java/lib /lib
#
# Mount / read only
$busybox mount -o ro,remount /
#
#
#
# (The Android Shell doesn't like empty lines)
TheDiamondYT1 commented 7 years ago

I originally got those commands from (this app)(github repo)

vishalbiswas commented 7 years ago

@TheDiamondYT1 I'm more interested in how you compiled the files in java.zip. It will allow us to create a package for termux.

TheDiamondYT1 commented 7 years ago

@vishalbiswas I didnt. I got the compressed archive from the repo in my previous comment. For some reason this time around i cant get the correct permissions on the java binary.

java not working

BKAppsmart commented 7 years ago

Is this happening? - referring to javac in termux? If it is ace, but if not is there some other solution to compiling java files and allowing them to run within the termux environment? Sorry if this question has been asked and answered elsewhere I found a few things but nothing really covered it fully... thanks!

TheDiamondYT1 commented 7 years ago

If we can get a JRE working maven will easily run

TheDiamondYT1 commented 7 years ago

Done.

alt

TheDiamondYT1 commented 7 years ago

However, there are some interesting things to note:

vishalbiswas commented 7 years ago

Compiling openjdk fails at shm support. That's the roadblock I'm stumped at.

TheDiamondYT1 commented 7 years ago

@vishalbiswas Try https://github.com/pelya/android-shmem

vishalbiswas commented 7 years ago

@TheDiamondYT1 already tried. That implementation isn't complete. Semaphore functions are missing which are required for ipc

vishalbiswas commented 7 years ago

http://openjdk.java.net/projects/mobile/android.html This documents how to cross compile OpenJDK 9 for Android. But I just can't get the build to succeed.

lingnand commented 7 years ago

If this works that would open a new world of possibilities.. @vishalbiswas Do you still remember what the error is from the build?

vishalbiswas commented 7 years ago

@lynnard undefined reference to SHMLBA and semop ------------------------------EDIT------------------------------- Exact errors:

hotspot/src/os/linux/vm/os_linux.cpp: In function 'char* shmat_with_alignment(int, size_t, size_t)':
hotspot/src/os/linux/vm/os_linux.cpp:3391:35: error: 'SHMLBA' was not declared in this scope
   if (!is_size_aligned(alignment, SHMLBA)) {
                                   ^
hotspot/src/os/linux/vm/os_linux.cpp: In function 'char* shmat_at_address(int, char*)':
hotspot/src/os/linux/vm/os_linux.cpp:3427:33: error: 'SHMLBA' was not declared in this scope
   if (!is_ptr_aligned(req_addr, SHMLBA)) {
                                 ^
hotspot/src/os/linux/vm/os_linux.cpp: In static member function 'static int os::loadavg(double*, int)':
hotspot/src/os/linux/vm/os_linux.cpp:5454:10: error: '::getloadavg' has not been declared
   return ::getloadavg(loadavg, nelem);
          ^
hotspot/src/os/linux/vm/os_linux.cpp: In function 'timespec get_mtime(const char*)':
hotspot/src/os/linux/vm/os_linux.cpp:6082:13: error: 'struct stat' has no member named 'st_mtim'
   return st.st_mtim;

semop and other semaphore functions are available as a syscall. I'm using them.

vishalbiswas commented 7 years ago

Managed to get past these errors with some hacks. Now the build fails for fatal error: langinfo.h not found

TheDiamondYT1 commented 7 years ago

Why is android so difficult

Harshiv-Patel commented 7 years ago

Terminal IDE uses a different approach, rather than compiling the native package for java command via c/cpp, spartacus rex uses an internal classLoader which uses android's dalvikvm API "dalvik.system.DexClassLoader" with reflection and a custom dexClassLoader class, ALL in java, although you need to check for lots of errors and exceptions. I don't have experience with java reflection APIs so can't do much. If you guys have time, please have a look at the two java classes from Terminal IDE: java.java and dexClassLoader.java, Please have a look at this method,it requires a script to invoke and is abit unsafe but I hope someone can work around with it and we'd have a working java classloder to load and run most of the JDK toola like jar,javac,javap etc.

vishalbiswas commented 7 years ago

@Harshiv-Patel Reflection? So, does that mean it has a large performance hit? OpenJDK 9 will have native support for Android. In fact, preliminary code for compilation for Android has already been added. I'm waiting for them to tag the next development release so that I can update the disabled openjdk package in the repo.

Harshiv-Patel commented 7 years ago

Terminal IDE does lag a little(~1500-2000ms) for the first time, when I load the tools.jar(~1.8MB, stripped, contains dexed classes from original tools.jar+resources for jar,javac and some other tools from JDK so it would make a difference when working with larger libraries I guess. Native support for Android sounds really good, it would be great.

MarijnS95 commented 7 years ago

With reagards to running java code on your phone through termux, this seems relatively easy to set up. I used jack (apt install jack) to compila java code to a dex, and then used dalvikvm to run it:

mkdir -p dex
jack --output-dex dex $(find -name *java)
dalvikvm -cp dex/classes.dex <name of your class>

So far I've only tried some really simple code, I'll experiment to see how far this works 😄 (requires no root btw ;))

TheDiamondYT1 commented 7 years ago

@vishalbiswas I have got the JRE 8 working on Android by downloading the files from a guys Dropbox folder. He claims its straight from the oracle website but the folder structure isint the same. It requires root and you have to mount some stuff but it does work

Harshiv-Patel commented 7 years ago

@vishalbiswas , Did you solved that header file not found error? We can already load the (dexed)Java classes via dalvikvm as suggested by @MarijnS95, and most of the JDK tools are written in java, so I think that we can just dex the classes from JDK's tools.jar and load them via dalvikvm, mostly we do not need the entire Java SE JRE 8(not sure though), JDK tools are sufficient for most of the android projects. @TheDiamondYT1 , root requirement and remounting method can be unsafe for noobies(like me) and we need a package that can be added to termux via apt install package_name.

TheDiamondYT1 commented 7 years ago

I know I was just sharing how I can run jar files

vishalbiswas commented 7 years ago

The latest source already deals with these issues, but there are others. Zero interpreter on arm has real bad performance

Harshiv-Patel commented 7 years ago

Android already has it's class loader optimized by AndroidRuntime, all they needed was to replace native Linux APIs with those of android's native API, using an interpreter is really bad idea when you can just dex your java classes as most of the java SE libraries are in java. Anyway what java command does is to load&run code from some archive which dalvikvm does for us, we can then load the dexed tools.jar and other Java SE libraries to be used with android and load them via dalvikvm. I've already dexed some tools like javac and jar, if I get success with javah,javadoc and javap, I'll post the scripts and dexed tools.jar for anyone interested to try out and test ,in next weekend.

vishalbiswas commented 7 years ago

Zero interpreter is only needed for arm devices. x86 devices can use the client jvm. Does dalvik has the complete java api? What about running apps that require a jdk? I want to ultimately be able to run gradle and build Android apps on Termux as if it was a real Linux box.

Harshiv-Patel commented 7 years ago
  1. I think there are very few devices running x86(not sure though) :confused:
  2. Yeh dalvikvm only has limited set of libraries, but most of the libraries can just be dexed with dx and should work on android that way, I guess :smirk:
  3. So you want to add JRE to android libraries, as done by @TheDiamondYT1, somewhat harder to accomplish but very useful once done :grinning:
vishalbiswas commented 7 years ago
  1. No. I want a standard java installation which I can use to compile Android apps on device painlessly.
TheDiamondYT1 commented 7 years ago

So the JDK (which also happens to include the JRE)

MarijnS95 commented 7 years ago

@vishalbiswas I've been fiddling with that for the past two days, and it turns out to be pretty simple (and is able to compile standard java applications as well, as mentioned in my previous comment). I only have apps that consist of a couple java files, some native C++ code through jni, and a simple makefile to invoke the compiler/packer. Nothing fancy, so if your apps include a lot of configuration with gradle, custom build tools etc, this might be more difficult ;)

The only thing I'm still working on is seamlessly installing the application and figuring out how to debug it (see the logs, for instance). But I'll save that for a more appropriate topic. Nevertheless, here's a rough outline of the steps involved:

Harshiv-Patel commented 7 years ago

@MarijnS95 lastly, we combine all these steps into a bash script and some extra features like

Porting the entire JDK to Android and then using Gradle is something that takes lots of experience and time AnD more space on device, It would take some time to write and debug the script, I'll probably give it a try.

Edit: That @joakim-noah has already built a script for native apps I guess.

TheDiamondYT1 commented 7 years ago

I made a simple script to compile Android apps

https://github.com/TheDiamondYT1/termux-buildapk

Harshiv-Patel commented 7 years ago

I've dexed some JDK tools and got them working. 'java' command isn't the full fledged desktop version, it's just a plain dumb class loader. Rest of tools are almost original. Running them on Samsung's Marshmallow Stock ROM may give errors, haven't tested yet on android 6+, do give it a try. Zip file: JDK_tools.zip

Harshiv-Patel commented 7 years ago

I've dexed some JDK tools and got them working. 'java' command isn't >the full fledged desktop version, it's just a plain dumb class loader. Rest >of tools are almost original. Running them on Samsung's Marshmallow >Stock ROM may give errors, haven't tested yet on android 6+, do give it a >try.

Did anyone looked at the zip ? It's about a month or so, anyone please look at it.

nospam2k commented 7 years ago

I'm using a Nexus 6p on 7.1.2.

I just copied into ~ and chmod +x on java. Ran ./java from the ~/JDK_Tools:

java.lang.ClassNotFoundException: Didn't find class "com.hrp.java" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) Exception in thread "main" java.lang.ClassNotFoundException: Didn't find class "com.hrp.java" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

PrimeEuler commented 7 years ago

Note 4 on 6.0.1 java -v -jar kafka_2.11-0.10.2.0.jar kafka ODEX_FOLDER : /data/data/com.termux/files/usr/var/android JAR/DEX FILE : kafka_2.11-0.10.2.0.jar CLASSNAME : kafka My path is: kafka_2.11-0.10.2.0.jar Output file will be /data/data/com.termux/files/usr/var/android/kafka_2.11-0.10.2.0.dex Failed loadDex 'kafka_2.11-0.10.2.0.jar': java.io.IOException: No original dex files found for dex location /data/data/com.termux/files/home/kafka_2.11-0.10.2.0/libs/kafka_2.11-0.10.2.0.jar Native lib path 0: /vendor/lib/ Native lib path 1: /system/lib/ DexClassLoader com.hrp.dexclassloader@3ea08f7: findClass 'kafka' Now searching: kafka_2.11-0.10.2.0.jar java.lang.ClassNotFoundException: kafka in loader com.hrp.dexclassloader@3ea08f7 at com.hrp.dexclassloader.findClass(dexclassloader.java:258) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at com.hrp.java.main(java.java:90) Suppressed: java.lang.ClassNotFoundException: Didn't find class "kafka" on path: DexPathList[[zip file "/data/data/com.termux/files/usr/bin/loader.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 2 more Suppressed: java.lang.ClassNotFoundException: kafka at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 3 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available Usage : java -v -jar [List of Jar files] CLASSNAME $

izackp commented 7 years ago

I was able to get Java working.

This is all in the root user space tho. I'm sure its possible to get it to work outside of it.

As a side note:

Harshiv-Patel commented 7 years ago

I'm using a Nexus 6p on 7.1.2.

Note 4 on 6.0.1

Sorry Guys, I haven't tried to run it on Android Lollipop 5.1.1+, it doesn't seem worth trying as Open JDK would be available on android in future. Android requires dex files to run them, usually "classes.dex" is required, you can't run normal java class files or ".jar" files containing classes, they need to be "dexed" first, using dx tool.

SDRausty commented 7 years ago

There are some Android apps which build quit well on device with the tools we have at our disposal today! Some of these apps are quite complex. Others are very entertaining. While others are very simple, mere fragments of apps that are complete in themselves. A new repository has just been opened with instructions and scripts that build Android apps on device (a smartphone).

Have you ever wanted to build your own Android application? Now you can easily compile these selected applications that have already been successfully built and installed on device.

Enjoy this project! Your feedback is welcome at either the wiki, the issues pages or through the code itself.

screenshot_20170529-034309

sirmordred commented 7 years ago

ok here https://github.com/sirmordred/JarClassLoader it can be beneficial for you guys

sirmordred commented 7 years ago

Info: i have a dx tool which works on android directly (compiled from latest dx sources) and have ecj compiler which works on android directly (latest version ecj)

the big disadvantage of ecj compiler is that it needs "androd.jar" as bootclasspath (34mb) (and it must be placed somewhere) if anyone have a standalone ecj compiler sources? maybe we can fuse both of jars (android.jar and ecj.jar) and create standalone android java compiler and handle that disadvantage

Harshiv-Patel commented 7 years ago

the big disadvantage of ecj compiler is that it needs "androd.jar" as bootclasspath (34mb) (and it must be placed somewhere)

You can place it in your internal storage somewhere, android.jar only requires to have read permission. I've put them in "~/storage/external-1/" and it works fine and then I added it's path to bashrc export droidjar=path/to/jar this can save some megabytes as long as it's only required to have read permission. I think we could place all jdk9 libraries and other such files like this, it would reduce the memory footprint considerably.

Harshiv-Patel commented 7 years ago

Info: i have a dx tool which works on android directly (compiled from latest dx sources)

  1. which version? 1.12 or higher?
  2. does it work with android.jar API level 22 or higher?
sirmordred commented 7 years ago

Yeah its 1.12 version And yeah im on marshmallow(23) its working great

sirmordred commented 7 years ago

for the ecj compiler, im currently trying to pack them, i mean packing android.jar and ecj.jar and create standalone aar library, i put into assets and compiler will copy from assets into application's internal folder (data/data/com.xyz.. ) via getApplicationContext,getFilesDir.getAbsolutePath

SDRausty commented 7 years ago

@AravK Java, javac and jar available through:

TermuxArch

Now you can install Arch Linux in Termux 📲 on your smartphone and tablet. More information at https://sdrausty.github.io/TermuxArch

Java is available through https://sdrausty.github.io/TermuxArch/ Just follow the easy install instructions at https://sdrausty.github.io/TermuxArch/docs/install or simply copy and paste the following into your Termux window:

cd && git clone https://github.com/sdrausty/TermuxArch
./TermuxArch/setupTermuxArch.sh

Read more about the Arch Linux package manager at duckduckgo arch+linux+install+java+pacman.

To install java run pacman -Syy; pacman -Su; pacman -S jdk8-openjdk

screenshot_20171109-220204 screenshot_20171109-222326