linux/Documentation/java.txt
<<
>>
Prefs
   1               Java(tm) Binary Kernel Support for Linux v1.03
   2               ----------------------------------------------
   3
   4Linux beats them ALL! While all other OS's are TALKING about direct
   5support of Java Binaries in the OS, Linux is doing it!
   6
   7You can execute Java applications and Java Applets just like any
   8other program after you have done the following:
   9
  101) You MUST FIRST install the Java Developers Kit for Linux.
  11   The Java on Linux HOWTO gives the details on getting and
  12   installing this. This HOWTO can be found at:
  13
  14        ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Java-HOWTO
  15
  16   You should also set up a reasonable CLASSPATH environment
  17   variable to use Java applications that make use of any
  18   nonstandard classes (not included in the same directory
  19   as the application itself).
  20
  212) You have to compile BINFMT_MISC either as a module or into
  22   the kernel (CONFIG_BINFMT_MISC) and set it up properly.
  23   If you choose to compile it as a module, you will have
  24   to insert it manually with modprobe/insmod, as kmod
  25   cannot easily be supported with binfmt_misc. 
  26   Read the file 'binfmt_misc.txt' in this directory to know
  27   more about the configuration process.
  28
  293) Add the following configuration items to binfmt_misc
  30   (you should really have read binfmt_misc.txt now):
  31   support for Java applications:
  32     ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:'
  33   support for executable Jar files:
  34     ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:'
  35   support for Java Applets:
  36     ':Applet:E::html::/usr/bin/appletviewer:'
  37   or the following, if you want to be more selective:
  38     ':Applet:M::<!--applet::/usr/bin/appletviewer:'
  39
  40   Of course you have to fix the path names. The path/file names given in this
  41   document match the Debian 2.1 system. (i.e. jdk installed in /usr,
  42   custom wrappers from this document in /usr/local)
  43
  44   Note, that for the more selective applet support you have to modify
  45   existing html-files to contain <!--applet--> in the first line
  46   ('<' has to be the first character!) to let this work!
  47
  48   For the compiled Java programs you need a wrapper script like the
  49   following (this is because Java is broken in case of the filename
  50   handling), again fix the path names, both in the script and in the
  51   above given configuration string.
  52
  53   You, too, need the little program after the script. Compile like
  54   gcc -O2 -o javaclassname javaclassname.c
  55   and stick it to /usr/local/bin.
  56
  57   Both the javawrapper shellscript and the javaclassname program
  58   were supplied by Colin J. Watson <cjw44@cam.ac.uk>.
  59
  60====================== Cut here ===================
  61#!/bin/bash
  62# /usr/local/bin/javawrapper - the wrapper for binfmt_misc/java
  63
  64if [ -z "$1" ]; then
  65        exec 1>&2
  66        echo Usage: $0 class-file
  67        exit 1
  68fi
  69
  70CLASS=$1
  71FQCLASS=`/usr/local/bin/javaclassname $1`
  72FQCLASSN=`echo $FQCLASS | sed -e 's/^.*\.\([^.]*\)$/\1/'`
  73FQCLASSP=`echo $FQCLASS | sed -e 's-\.-/-g' -e 's-^[^/]*$--' -e 's-/[^/]*$--'`
  74
  75# for example:
  76# CLASS=Test.class
  77# FQCLASS=foo.bar.Test
  78# FQCLASSN=Test
  79# FQCLASSP=foo/bar
  80
  81unset CLASSBASE
  82
  83declare -i LINKLEVEL=0
  84
  85while :; do
  86        if [ "`basename $CLASS .class`" == "$FQCLASSN" ]; then
  87                # See if this directory works straight off
  88                cd -L `dirname $CLASS`
  89                CLASSDIR=$PWD
  90                cd $OLDPWD
  91                if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
  92                        CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
  93                        break;
  94                fi
  95                # Try dereferencing the directory name
  96                cd -P `dirname $CLASS`
  97                CLASSDIR=$PWD
  98                cd $OLDPWD
  99                if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
 100                        CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
 101                        break;
 102                fi
 103                # If no other possible filename exists
 104                if [ ! -L $CLASS ]; then
 105                        exec 1>&2
 106                        echo $0:
 107                        echo "  $CLASS should be in a" \
 108                             "directory tree called $FQCLASSP"
 109                        exit 1
 110                fi
 111        fi
 112        if [ ! -L $CLASS ]; then break; fi
 113        # Go down one more level of symbolic links
 114        let LINKLEVEL+=1
 115        if [ $LINKLEVEL -gt 5 ]; then
 116                exec 1>&2
 117                echo $0:
 118                echo "  Too many symbolic links encountered"
 119                exit 1
 120        fi
 121        CLASS=`ls --color=no -l $CLASS | sed -e 's/^.* \([^ ]*\)$/\1/'`
 122done
 123
 124if [ -z "$CLASSBASE" ]; then
 125        if [ -z "$FQCLASSP" ]; then
 126                GOODNAME=$FQCLASSN.class
 127        else
 128                GOODNAME=$FQCLASSP/$FQCLASSN.class
 129        fi
 130        exec 1>&2
 131        echo $0:
 132        echo "  $FQCLASS should be in a file called $GOODNAME"
 133        exit 1
 134fi
 135
 136if ! echo $CLASSPATH | grep -q "^\(.*:\)*$CLASSBASE\(:.*\)*"; then
 137        # class is not in CLASSPATH, so prepend dir of class to CLASSPATH
 138        if [ -z "${CLASSPATH}" ] ; then
 139                export CLASSPATH=$CLASSBASE
 140        else
 141                export CLASSPATH=$CLASSBASE:$CLASSPATH
 142        fi
 143fi
 144
 145shift
 146/usr/bin/java $FQCLASS "$@"
 147====================== Cut here ===================
 148
 149
 150====================== Cut here ===================
 151/* javaclassname.c
 152 *
 153 * Extracts the class name from a Java class file; intended for use in a Java
 154 * wrapper of the type supported by the binfmt_misc option in the Linux kernel.
 155 *
 156 * Copyright (C) 1999 Colin J. Watson <cjw44@cam.ac.uk>.
 157 *
 158 * This program is free software; you can redistribute it and/or modify
 159 * it under the terms of the GNU General Public License as published by
 160 * the Free Software Foundation; either version 2 of the License, or
 161 * (at your option) any later version.
 162 *
 163 * This program is distributed in the hope that it will be useful,
 164 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 165 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 166 * GNU General Public License for more details.
 167 *
 168 * You should have received a copy of the GNU General Public License
 169 * along with this program; if not, write to the Free Software
 170 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 171 */
 172
 173#include <stdlib.h>
 174#include <stdio.h>
 175#include <stdarg.h>
 176#include <sys/types.h>
 177
 178/* From Sun's Java VM Specification, as tag entries in the constant pool. */
 179
 180#define CP_UTF8 1
 181#define CP_INTEGER 3
 182#define CP_FLOAT 4
 183#define CP_LONG 5
 184#define CP_DOUBLE 6
 185#define CP_CLASS 7
 186#define CP_STRING 8
 187#define CP_FIELDREF 9
 188#define CP_METHODREF 10
 189#define CP_INTERFACEMETHODREF 11
 190#define CP_NAMEANDTYPE 12
 191
 192/* Define some commonly used error messages */
 193
 194#define seek_error() error("%s: Cannot seek\n", program)
 195#define corrupt_error() error("%s: Class file corrupt\n", program)
 196#define eof_error() error("%s: Unexpected end of file\n", program)
 197#define utf8_error() error("%s: Only ASCII 1-255 supported\n", program);
 198
 199char *program;
 200
 201long *pool;
 202
 203u_int8_t read_8(FILE *classfile);
 204u_int16_t read_16(FILE *classfile);
 205void skip_constant(FILE *classfile, u_int16_t *cur);
 206void error(const char *format, ...);
 207int main(int argc, char **argv);
 208
 209/* Reads in an unsigned 8-bit integer. */
 210u_int8_t read_8(FILE *classfile)
 211{
 212        int b = fgetc(classfile);
 213        if(b == EOF)
 214                eof_error();
 215        return (u_int8_t)b;
 216}
 217
 218/* Reads in an unsigned 16-bit integer. */
 219u_int16_t read_16(FILE *classfile)
 220{
 221        int b1, b2;
 222        b1 = fgetc(classfile);
 223        if(b1 == EOF)
 224                eof_error();
 225        b2 = fgetc(classfile);
 226        if(b2 == EOF)
 227                eof_error();
 228        return (u_int16_t)((b1 << 8) | b2);
 229}
 230
 231/* Reads in a value from the constant pool. */
 232void skip_constant(FILE *classfile, u_int16_t *cur)
 233{
 234        u_int16_t len;
 235        int seekerr = 1;
 236        pool[*cur] = ftell(classfile);
 237        switch(read_8(classfile))
 238        {
 239        case CP_UTF8:
 240                len = read_16(classfile);
 241                seekerr = fseek(classfile, len, SEEK_CUR);
 242                break;
 243        case CP_CLASS:
 244        case CP_STRING:
 245                seekerr = fseek(classfile, 2, SEEK_CUR);
 246                break;
 247        case CP_INTEGER:
 248        case CP_FLOAT:
 249        case CP_FIELDREF:
 250        case CP_METHODREF:
 251        case CP_INTERFACEMETHODREF:
 252        case CP_NAMEANDTYPE:
 253                seekerr = fseek(classfile, 4, SEEK_CUR);
 254                break;
 255        case CP_LONG:
 256        case CP_DOUBLE:
 257                seekerr = fseek(classfile, 8, SEEK_CUR);
 258                ++(*cur);
 259                break;
 260        default:
 261                corrupt_error();
 262        }
 263        if(seekerr)
 264                seek_error();
 265}
 266
 267void error(const char *format, ...)
 268{
 269        va_list ap;
 270        va_start(ap, format);
 271        vfprintf(stderr, format, ap);
 272        va_end(ap);
 273        exit(1);
 274}
 275
 276int main(int argc, char **argv)
 277{
 278        FILE *classfile;
 279        u_int16_t cp_count, i, this_class, classinfo_ptr;
 280        u_int8_t length;
 281
 282        program = argv[0];
 283
 284        if(!argv[1])
 285                error("%s: Missing input file\n", program);
 286        classfile = fopen(argv[1], "rb");
 287        if(!classfile)
 288                error("%s: Error opening %s\n", program, argv[1]);
 289
 290        if(fseek(classfile, 8, SEEK_SET))  /* skip magic and version numbers */
 291                seek_error();
 292        cp_count = read_16(classfile);
 293        pool = calloc(cp_count, sizeof(long));
 294        if(!pool)
 295                error("%s: Out of memory for constant pool\n", program);
 296
 297        for(i = 1; i < cp_count; ++i)
 298                skip_constant(classfile, &i);
 299        if(fseek(classfile, 2, SEEK_CUR))       /* skip access flags */
 300                seek_error();
 301
 302        this_class = read_16(classfile);
 303        if(this_class < 1 || this_class >= cp_count)
 304                corrupt_error();
 305        if(!pool[this_class] || pool[this_class] == -1)
 306                corrupt_error();
 307        if(fseek(classfile, pool[this_class] + 1, SEEK_SET))
 308                seek_error();
 309
 310        classinfo_ptr = read_16(classfile);
 311        if(classinfo_ptr < 1 || classinfo_ptr >= cp_count)
 312                corrupt_error();
 313        if(!pool[classinfo_ptr] || pool[classinfo_ptr] == -1)
 314                H24java.txt#L310" id="L310" class="line"mentscumentation/jav1"> 311  t"L153"> 2nEj="L160">27m=27mi href="wline" name="L303"> 3032">href="Documentation/java.txt#L3r=2xt#L208" id="L208" class="line" name="L208"> 208
.txt#L296" id="L296" class="line" name=3d 16-bit 3nteger. */
 219u_int16_t read_36(FIL32"L290"> 290      ne" name=xmentatio8java.txt#L309" id="L309" class="line" name="L30 class="l3ne" name="L220"> 2203
 297< 0x80)line!x#L296" id="L296" class="line" name=3do_ptr >   int b1, b2;
 297< 0xE0)la> 0xC0#L296" id="L296" class="line" name=3dclassinfo   if(b1 == EOF)
 290      ne" name=ymentatio8java.txt#L309" id="L309" class="line" name="L30 ef="Docum b2 = fgetc(classfile);
3a hre3="Documentation/java.tn/java.te="L311"L31(y> 297< 0xC0)la> 0x80#L296" id="L296" class="line" name=3da>
 108       n/java.te="L311"L94"cmen1(x> 297< 0x1f)ass="line"6)"> (y> 297< 0x3f309" id="L309" class="line" name="L30     3  return (u_int16_t)((b13<&33e" name="L99">  99       n/java.te="L311"L31c) putcume1c)09" id="L309" class="line" name="L30#define eof09" id="L309" class="line" name="L30  91            "Documentation/java.txt#L273" id="L3 */
  92                          139"a>#define eof09" id="L309" class="line" name="L30<="Documenp_constant(FILE *classfi3e, u_33"Documentation/java.te="L311""Documentation/java.txt#L273" id="L3 classinfo name="L233"> 233{
<3 href33.txt#L104" id="L104" class="li39"a>#define eof09" id="L309" class="line" name="L30<" class="      u_int16_t len;
 238        {
3a hre3="Documentatioputcume120 1493a href="D3cumentation/java.txt#L243" id=34 295                error("!= fseek(c3assfile, 2, SEEK_CUR);
<3 href34id="L208" class="line" name="L208"> 208< 246 3              break;
 1493as_class]       case CP_INTEGER:
<3 href3"Docum#!a hrebash149" class="line" name="L149"> 1493248  3     case CP_FLOAT:
 153 153 14932ase CP_U3 case CP_FIELDREF:
fi

 1493a href="D3cumentation/java.txt#L253" id=35        fi
 254                break;
3a hre3="DocuNow sribuy chmod +xtatio.s="line.f=r s="line.htmlname=s.txt#w294"toDocumute FITNESS FOR A PARTICULAR PURPOSE. 3"> 255 3cumentation/java.txt#L253" id=3L257" .s="liot;L94o a>
 127       3seek(clas3file, 8, SEEK_CUR);
in id="L94original .s="liot; " clas"line" name="L127"> 127       3258  3             ++(*cur);
<3 href3"Documadded=toDL160"href="Docudur id=ocumutiftware Foundation; either version 2 o3="L259"> 359                br3ak;
<36tation/java.txt#L300" id="L300" class="l60   3    default:
 2613/a>                corru3t_err3r();
 262        3
   3    if(seekerr)
 264          3     36 name="L285"> 285< HelloWorldljava.txt#L276" id="L276" class="lin3txt#L265"3id="L265" class="line" n3me="L36"Documentation/java.tplass="s"L27c 65}
void error(const3char 36" name="L108"> 108 2683
 3    va_list ap;
xt#L259" id="L259" class="line" nam3      vfp3intf(stderr, format, ap)3
 273<3a>   3    exSet3ss="ocumuta2" c3xt#L259" id="L259" class="line" nam3 e" name=3id="L274" class="line" n3me="L37 name="L285"> hmod 755 HelloWorld.lass="line" name="L127"> 127       3e" name="3275"> 275
 276int main(int3argc,3char *Aasst#L11ocumute itxt#L259" id="L259" class="line" nam3s="line" 3ame="L277"> 277{
 127       3 href="Do3umentation/java.txt#L2793 id="38; 8) | b2);
 280    3   u_3nt8_t ToDocumute.txt#LJ=r ame=s,=sribue. hmod  pro*.f=r ame=s.toDtion/jatation/java.txt#L280" id="L280" cl3s     vfp3me="L281"> 281
 282      3 prog3am = argv[0];./A53lava.txt.f=r149" class="line" name="L149"> 1493 class="l3ne" name="L283"> 2833 295                error("!s: Out o3 memory for constant poo3\n&qu3t;, pr


TL94original LXRation/javs name fa> http://s160ce"lige.ne4/projects/lxi">LXRa="Dounity, pr8" claion/eri ls="line" naa> a>}lto:lxi@lnded.no">lxi@lnded.no57" .
lxi.lnded.no kinduy hoslass naa> http://www.redp;&l-lndpro.no">Redp;<endpro AS, pr8"provid *Lnded ="liult id=lasso/er erros sxrvic=s.stioeuot;5.