^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) Java(tm) Binary Kernel Support for Linux v1.03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) ----------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Linux beats them ALL! While all other OS's are TALKING about direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) support of Java Binaries in the OS, Linux is doing it!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) You can execute Java applications and Java Applets just like any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) other program after you have done the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) 1) You MUST FIRST install the Java Developers Kit for Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) The Java on Linux HOWTO gives the details on getting and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) installing this. This HOWTO can be found at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Java-HOWTO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) You should also set up a reasonable CLASSPATH environment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) variable to use Java applications that make use of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) nonstandard classes (not included in the same directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) as the application itself).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 2) You have to compile BINFMT_MISC either as a module or into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) the kernel (``CONFIG_BINFMT_MISC``) and set it up properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) If you choose to compile it as a module, you will have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) to insert it manually with modprobe/insmod, as kmod
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) cannot easily be supported with binfmt_misc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) Read the file 'binfmt_misc.txt' in this directory to know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) more about the configuration process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 3) Add the following configuration items to binfmt_misc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) (you should really have read ``binfmt_misc.txt`` now):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) support for Java applications::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) support for executable Jar files::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) support for Java Applets::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ':Applet:E::html::/usr/bin/appletviewer:'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) or the following, if you want to be more selective::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ':Applet:M::<!--applet::/usr/bin/appletviewer:'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) Of course you have to fix the path names. The path/file names given in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) document match the Debian 2.1 system. (i.e. jdk installed in ``/usr``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) custom wrappers from this document in ``/usr/local``)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) Note, that for the more selective applet support you have to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) existing html-files to contain ``<!--applet-->`` in the first line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) (``<`` has to be the first character!) to let this work!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) For the compiled Java programs you need a wrapper script like the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) following (this is because Java is broken in case of the filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) handling), again fix the path names, both in the script and in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) above given configuration string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) You, too, need the little program after the script. Compile like::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) gcc -O2 -o javaclassname javaclassname.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) and stick it to ``/usr/local/bin``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) Both the javawrapper shellscript and the javaclassname program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) were supplied by Colin J. Watson <cjw44@cam.ac.uk>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) Javawrapper shell script:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .. code-block:: sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) # /usr/local/bin/javawrapper - the wrapper for binfmt_misc/java
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if [ -z "$1" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) exec 1>&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) echo Usage: $0 class-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) CLASS=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) FQCLASS=`/usr/local/bin/javaclassname $1`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) FQCLASSN=`echo $FQCLASS | sed -e 's/^.*\.\([^.]*\)$/\1/'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) FQCLASSP=`echo $FQCLASS | sed -e 's-\.-/-g' -e 's-^[^/]*$--' -e 's-/[^/]*$--'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) # for example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # CLASS=Test.class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) # FQCLASS=foo.bar.Test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # FQCLASSN=Test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # FQCLASSP=foo/bar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unset CLASSBASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) declare -i LINKLEVEL=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) while :; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if [ "`basename $CLASS .class`" == "$FQCLASSN" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) # See if this directory works straight off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) cd -L `dirname $CLASS`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) CLASSDIR=$PWD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cd $OLDPWD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) # Try dereferencing the directory name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) cd -P `dirname $CLASS`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) CLASSDIR=$PWD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cd $OLDPWD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) # If no other possible filename exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if [ ! -L $CLASS ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) exec 1>&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) echo $0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) echo " $CLASS should be in a" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "directory tree called $FQCLASSP"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if [ ! -L $CLASS ]; then break; fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) # Go down one more level of symbolic links
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) let LINKLEVEL+=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if [ $LINKLEVEL -gt 5 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) exec 1>&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) echo $0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) echo " Too many symbolic links encountered"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) CLASS=`ls --color=no -l $CLASS | sed -e 's/^.* \([^ ]*\)$/\1/'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if [ -z "$CLASSBASE" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if [ -z "$FQCLASSP" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) GOODNAME=$FQCLASSN.class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) GOODNAME=$FQCLASSP/$FQCLASSN.class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) exec 1>&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) echo $0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) echo " $FQCLASS should be in a file called $GOODNAME"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if ! echo $CLASSPATH | grep -q "^\(.*:\)*$CLASSBASE\(:.*\)*"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) # class is not in CLASSPATH, so prepend dir of class to CLASSPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if [ -z "${CLASSPATH}" ] ; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) export CLASSPATH=$CLASSBASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) export CLASSPATH=$CLASSBASE:$CLASSPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /usr/bin/java $FQCLASS "$@"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) javaclassname.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* javaclassname.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Extracts the class name from a Java class file; intended for use in a Java
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * wrapper of the type supported by the binfmt_misc option in the Linux kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Copyright (C) 1999 Colin J. Watson <cjw44@cam.ac.uk>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* From Sun's Java VM Specification, as tag entries in the constant pool. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #define CP_UTF8 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #define CP_INTEGER 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #define CP_FLOAT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #define CP_LONG 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #define CP_DOUBLE 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #define CP_CLASS 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define CP_STRING 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #define CP_FIELDREF 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #define CP_METHODREF 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #define CP_INTERFACEMETHODREF 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #define CP_NAMEANDTYPE 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #define CP_METHODHANDLE 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #define CP_METHODTYPE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #define CP_INVOKEDYNAMIC 18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* Define some commonly used error messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #define seek_error() error("%s: Cannot seek\n", program)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #define corrupt_error() error("%s: Class file corrupt\n", program)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #define eof_error() error("%s: Unexpected end of file\n", program)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #define utf8_error() error("%s: Only ASCII 1-255 supported\n", program);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) char *program;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) long *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) u_int8_t read_8(FILE *classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) u_int16_t read_16(FILE *classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) void skip_constant(FILE *classfile, u_int16_t *cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) void error(const char *format, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int main(int argc, char **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Reads in an unsigned 8-bit integer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u_int8_t read_8(FILE *classfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int b = fgetc(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if(b == EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) eof_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return (u_int8_t)b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Reads in an unsigned 16-bit integer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) u_int16_t read_16(FILE *classfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int b1, b2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) b1 = fgetc(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if(b1 == EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) eof_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) b2 = fgetc(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if(b2 == EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) eof_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return (u_int16_t)((b1 << 8) | b2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* Reads in a value from the constant pool. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) void skip_constant(FILE *classfile, u_int16_t *cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) u_int16_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int seekerr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) pool[*cur] = ftell(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) switch(read_8(classfile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case CP_UTF8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) len = read_16(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) seekerr = fseek(classfile, len, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case CP_CLASS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) case CP_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case CP_METHODTYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) seekerr = fseek(classfile, 2, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case CP_METHODHANDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) seekerr = fseek(classfile, 3, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case CP_INTEGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case CP_FLOAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case CP_FIELDREF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case CP_METHODREF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case CP_INTERFACEMETHODREF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) case CP_NAMEANDTYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) case CP_INVOKEDYNAMIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) seekerr = fseek(classfile, 4, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case CP_LONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case CP_DOUBLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) seekerr = fseek(classfile, 8, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ++(*cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) corrupt_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if(seekerr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) seek_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) void error(const char *format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) va_start(ap, format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) vfprintf(stderr, format, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) FILE *classfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) u_int16_t cp_count, i, this_class, classinfo_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) u_int8_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) program = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if(!argv[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) error("%s: Missing input file\n", program);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) classfile = fopen(argv[1], "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if(!classfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) error("%s: Error opening %s\n", program, argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if(fseek(classfile, 8, SEEK_SET)) /* skip magic and version numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) seek_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) cp_count = read_16(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) pool = calloc(cp_count, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if(!pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) error("%s: Out of memory for constant pool\n", program);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) for(i = 1; i < cp_count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) skip_constant(classfile, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if(fseek(classfile, 2, SEEK_CUR)) /* skip access flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) seek_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) this_class = read_16(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if(this_class < 1 || this_class >= cp_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) corrupt_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if(!pool[this_class] || pool[this_class] == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) corrupt_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if(fseek(classfile, pool[this_class] + 1, SEEK_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) seek_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) classinfo_ptr = read_16(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if(classinfo_ptr < 1 || classinfo_ptr >= cp_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) corrupt_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if(!pool[classinfo_ptr] || pool[classinfo_ptr] == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) corrupt_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if(fseek(classfile, pool[classinfo_ptr] + 1, SEEK_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) seek_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) length = read_16(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) for(i = 0; i < length; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) u_int8_t x = read_8(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if((x & 0x80) || !x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if((x & 0xE0) == 0xC0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) u_int8_t y = read_8(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if((y & 0xC0) == 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int c = ((x & 0x1f) << 6) + (y & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if(c) putchar(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) else utf8_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) else utf8_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) else utf8_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) else if(x == '/') putchar('.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) else putchar(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) putchar('\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) free(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) fclose(classfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) jarwrapper::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) # /usr/local/java/bin/jarwrapper - the wrapper for binfmt_misc/jar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) java -jar $1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) Now simply ``chmod +x`` the ``.class``, ``.jar`` and/or ``.html`` files you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) want to execute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) To add a Java program to your path best put a symbolic link to the main
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) .class file into /usr/bin (or another place you like) omitting the .class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) extension. The directory containing the original .class file will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) added to your CLASSPATH during execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) To test your new setup, enter in the following simple Java app, and name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) it "HelloWorld.java":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .. code-block:: java
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) class HelloWorld {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) public static void main(String args[]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) System.out.println("Hello World!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) Now compile the application with::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) javac HelloWorld.java
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) Set the executable permissions of the binary file, with::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) chmod 755 HelloWorld.class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) And then execute it::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ./HelloWorld.class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) To execute Java Jar files, simple chmod the ``*.jar`` files to include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) the execution bit, then just do::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ./Application.jar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) To execute Java Applets, simple chmod the ``*.html`` files to include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) the execution bit, then just do::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ./Applet.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) originally by Brian A. Lantz, brian@lantz.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) heavily edited for binfmt_misc by Richard Günther
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) new scripts by Colin J. Watson <cjw44@cam.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) added executable Jar file support by Kurt Huwig <kurt@iku-netz.de>