Skip to content

Instantly share code, notes, and snippets.

@simonesestito
Last active May 29, 2023 18:59
Show Gist options
  • Save simonesestito/89bb66e6e08b8662a40fc42b822090e6 to your computer and use it in GitHub Desktop.
Save simonesestito/89bb66e6e08b8662a40fc42b822090e6 to your computer and use it in GitHub Desktop.
Termux script to run Java programs
#!/data/data/com.termux/files/usr/bin/bash
#
# HOW TO INSTALL
#
# Run this command in Termux:
#
# curl https://gist.githubusercontent.com/simonesestito/89bb66e6e08b8662a40fc42b822090e6/raw/setup.sh | bash
#
WORKDIR=`mktemp -d`
LAUNCHDIR=`pwd`
status(){
echo -e "\033[1;32m// $*...\033[0m"
}
stop(){
echo -e "\033[1;37;41m(!) $*\033[0m"
clean
exit 1
}
clean(){
rm -rf $WORKDIR
}
# Checking files
if [ $# -eq 0 ]; then
stop Usage: java FILES
fi
for f in $*; {
if [ ! -f $f ]; then
stop File $f is missing
fi
}
status Compiling
ecj $* -d $WORKDIR || stop Compilation error
# Building dex file
cd $WORKDIR
classes=`find * -type f -name '*.class'`
dx --dex --output out.dex $classes \
2> /dev/null || \
stop Error building dex file for DalvikVM
cd $LAUNCHDIR
# Startinf
executed=0
exec 5>&1
for f in $classes; {
clear
main=`echo $f | cut -f 1 -d '.'`
output=`dalvikvm -cp $WORKDIR/out.dex $main 2>&1 | tee /dev/fd/5`
if ! echo "$output" | grep 'Unable to find static main(String' > /dev/null; then
executed=1
break
fi
}
if [ $executed -eq 0 ]; then
stop "Execution error. Please check that
- 1 class has a correct main() method
- it doesn't throw an exception in main()"
fi
clean
#!/data/data/com.termux/files/usr/bin/bash
#
# Setup script
# You shouldn't run this script manually
#
GIST_URL=https://gist.githubusercontent.com/simonesestito/89bb66e6e08b8662a40fc42b822090e6/raw/java.sh
SH_LOCAT=~/.java.sh
echo Downloading files...
curl $GIST_URL \
> $SH_LOCAT \
2> /dev/null
chmod +x $SH_LOCAT
ALIAS_STR="alias java=$SH_LOCAT"
if grep -E "^$ALIAS_STR$" ~/.bashrc > /dev/null; then
echo Alias set skipped.
else
echo Settings Bash alias...
echo "alias java=$SH_LOCAT" >> ~/.bashrc
fi
echo Installing packages...
pkg install -y \
ecj \
dx \
> /dev/null \
2>&1
# DONE
echo
echo "----------"
echo
echo "Java Termux script installed"
echo -e "\033[1mRestart Termux to use it.\033[0m"
echo
echo -e "\033[1mUsage: java <file list>\033[0m"
echo "Example: java Main.java ClassA.java"
echo
echo "----------"
echo
@ROBGUI09
Copy link

Is it able to reach getApplicationContext() with this script?

@simonesestito
Copy link
Author

Is it able to reach getApplicationContext() with this script?

Never tried, but I don't think so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment