本帖最后由 happy886rr 于 2017-8-6 23:06 编辑
对于java程序是不能在安卓系统下直接运行的。我们需要用dx工具将java字节码转化为安卓虚拟机字节码(命令如下):- @echo off
- pushd "%~dp0wkpath"
- echo ----------------Jar2dex Script----------------
- ::: 2>nul java -version || echo Needs Java && pause && exit
-
- if /i "%~x1"==".jar" (
- 2>nul del /f /q ".\classes.dex"
- echo 正在生成DEX文件...
- java -jar dx.jar --dex --output=classes.dex "%~f1"
- if exist ".\classes.dex" (
- echo 正在添加dex文件到 "%~f1"
- aapt add "%~f1" classes.dex
- )
- ) else (
- echo 非jar包、或无输入文件.
- )
-
- if exist ".\classes.dex" (
- 2>nul del /f /q ".\classes.dex"
- )
- echo 执行完毕.
- echo ----------------------------------------------
- pause
复制代码 此时生成的jar包就具全平台运行性。但手机的模拟终端输入不便,因此 ej脚本 意在解决手机运行jar的繁琐输入。脚本内容如下:- #!/system/bin/sh
- # ej script by happy
- # Set the operating system type
- USE_ANDROID="true"
-
- if [ $# -ne 2 ]
- then
- echo "Usage: ej.sh [class path] [class name]"
- exit 1
- else
- echo [===* Welcome to use ejar script *===]
-
- export CLASSPATH="${1}"
-
- jarname=${1##*/}
- jarname=${jarname%.*}
-
- if [ "$jarname" == "" ]
- then
- echo "You must input a jar file!"
- exit 1
- fi
-
- if [ "$USE_ANDROID" == "true" ]
- then
- 2>/dev/null app_process "${1}" ${2}
- if [ $? -ne 0 ]
- then
- echo "May be your input is wrong, or chmod the file's permissions."
- exit 1
- fi
- else
- 2>/dev/null java -version
- if [ $? -ne 0 ]
- then
- echo You needs java!.
- exit 1
- fi
- fi
-
- while [ true ]
- do
- echo -n "[>>> ${jarname} "
- read args
-
- if [ "$args" == "--quit" ] || [ "$args" == "--exit" ]
- then
- break;
- fi
-
- if [ "$USE_ANDROID" == "true" ]
- then
- app_process "${1%/*}" ${2} ${args}
- else
- java -classpath "${1}" ${2} ${args}
- fi
- done
-
- echo -e "[<<<]"
- fi
复制代码 将脚本存为ej.sh,用adb命令将其推送到手机临时目录 /data/local/tmp,并设置权限为0755,命令如下:- adb kill-server
- adb wait-for-device
- adb push .\ej.sh /data/local/tmp
- chmod 0755 ./ej.sh
复制代码 首次运行,只需输入如下命令(./ej.sh [jar包的路径] [要执行的类]):- ./ej.sh jcalc.jar com.android.jcalc.Main
复制代码 运行图如下(图片均为外部链接):
之后每次运行,直接输入传递参数即可。演示如下:[>>> jcalc sin3*cbrt(e)
sin3*cbrt(e)
OriginalExp: sin3*cbrt(e)
Revpolish: 3 sin e cbrt *
Result: 0.196948836677
[>>> jcalc 5*(arctan(cos(sin(ln(lg(2.71828))))))
5*(arctan(cos(sin(ln(lg(2.71828))))))
OriginalExp: 5*(arctan(cos(sin(ln(lg(2.71828))))))
Revpolish: 5 2.71828 lg ln sin cos arctan *
Result: 3.178987410777
[>>> jcalc --quit
--quit
[<<<] 备注:(linux下亦可兼容)请使用 --quit 或 --exit 退出 ej.sh脚本 . |