1 Java(tm) Binary Kernel Support for Linux v1.03
2 ----------------------------------------------
4 Linux beats them ALL! While all other OS's are TALKING about direct
5 support of Java Binaries in the OS, Linux is doing it!
7 You can execute Java applications and Java Applets just like any
8 other program after you have done the following:
10 1) 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:
14 ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Java-HOWTO
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).
21 2) 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.
29 3) Add the following configuration items to binfmt_misc
30 (you should really have read ``binfmt_misc.txt`` now):
31 support for Java applications::
33 ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:'
35 support for executable Jar files::
37 ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:'
39 support for Java Applets::
41 ':Applet:E::html::/usr/bin/appletviewer:'
43 or the following, if you want to be more selective::
45 ':Applet:M::<!--applet::/usr/bin/appletviewer:'
47 Of course you have to fix the path names. The path/file names given in this
48 document match the Debian 2.1 system. (i.e. jdk installed in ``/usr``,
49 custom wrappers from this document in ``/usr/local``)
51 Note, that for the more selective applet support you have to modify
52 existing html-files to contain ``<!--applet-->`` in the first line
53 (``<`` has to be the first character!) to let this work!
55 For the compiled Java programs you need a wrapper script like the
56 following (this is because Java is broken in case of the filename
57 handling), again fix the path names, both in the script and in the
58 above given configuration string.
60 You, too, need the little program after the script. Compile like::
62 gcc -O2 -o javaclassname javaclassname.c
64 and stick it to ``/usr/local/bin``.
66 Both the javawrapper shellscript and the javaclassname program
67 were supplied by Colin J. Watson <cjw44@cam.ac.uk>.
69 Javawrapper shell script::
72 # /usr/local/bin/javawrapper - the wrapper for binfmt_misc/java
76 echo Usage: $0 class-file
81 FQCLASS=`/usr/local/bin/javaclassname $1`
82 FQCLASSN=`echo $FQCLASS | sed -e 's/^.*\.\([^.]*\)$/\1/'`
83 FQCLASSP=`echo $FQCLASS | sed -e 's-\.-/-g' -e 's-^[^/]*$--' -e 's-/[^/]*$--'`
87 # FQCLASS=foo.bar.Test
93 declare -i LINKLEVEL=0
96 if [ "`basename $CLASS .class`" == "$FQCLASSN" ]; then
97 # See if this directory works straight off
98 cd -L `dirname $CLASS`
101 if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
102 CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
105 # Try dereferencing the directory name
106 cd -P `dirname $CLASS`
109 if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
110 CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
113 # If no other possible filename exists
114 if [ ! -L $CLASS ]; then
117 echo " $CLASS should be in a" \
118 "directory tree called $FQCLASSP"
122 if [ ! -L $CLASS ]; then break; fi
123 # Go down one more level of symbolic links
125 if [ $LINKLEVEL -gt 5 ]; then
128 echo " Too many symbolic links encountered"
131 CLASS=`ls --color=no -l $CLASS | sed -e 's/^.* \([^ ]*\)$/\1/'`
134 if [ -z "$CLASSBASE" ]; then
135 if [ -z "$FQCLASSP" ]; then
136 GOODNAME=$FQCLASSN.class
138 GOODNAME=$FQCLASSP/$FQCLASSN.class
142 echo " $FQCLASS should be in a file called $GOODNAME"
146 if ! echo $CLASSPATH | grep -q "^\(.*:\)*$CLASSBASE\(:.*\)*"; then
147 # class is not in CLASSPATH, so prepend dir of class to CLASSPATH
148 if [ -z "${CLASSPATH}" ] ; then
149 export CLASSPATH=$CLASSBASE
151 export CLASSPATH=$CLASSBASE:$CLASSPATH
156 /usr/bin/java $FQCLASS "$@"
162 * Extracts the class name from a Java class file; intended for use in a Java
163 * wrapper of the type supported by the binfmt_misc option in the Linux kernel.
165 * Copyright (C) 1999 Colin J. Watson <cjw44@cam.ac.uk>.
167 * This program is free software; you can redistribute it and/or modify
168 * it under the terms of the GNU General Public License as published by
169 * the Free Software Foundation; either version 2 of the License, or
170 * (at your option) any later version.
172 * This program is distributed in the hope that it will be useful,
173 * but WITHOUT ANY WARRANTY; without even the implied warranty of
174 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175 * GNU General Public License for more details.
177 * You should have received a copy of the GNU General Public License
178 * along with this program; if not, write to the Free Software
179 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
185 #include <sys/types.h>
187 /* From Sun's Java VM Specification, as tag entries in the constant pool. */
196 #define CP_FIELDREF 9
197 #define CP_METHODREF 10
198 #define CP_INTERFACEMETHODREF 11
199 #define CP_NAMEANDTYPE 12
200 #define CP_METHODHANDLE 15
201 #define CP_METHODTYPE 16
202 #define CP_INVOKEDYNAMIC 18
204 /* Define some commonly used error messages */
206 #define seek_error() error("%s: Cannot seek\n", program)
207 #define corrupt_error() error("%s: Class file corrupt\n", program)
208 #define eof_error() error("%s: Unexpected end of file\n", program)
209 #define utf8_error() error("%s: Only ASCII 1-255 supported\n", program);
215 u_int8_t read_8(FILE *classfile);
216 u_int16_t read_16(FILE *classfile);
217 void skip_constant(FILE *classfile, u_int16_t *cur);
218 void error(const char *format, ...);
219 int main(int argc, char **argv);
221 /* Reads in an unsigned 8-bit integer. */
222 u_int8_t read_8(FILE *classfile)
224 int b = fgetc(classfile);
230 /* Reads in an unsigned 16-bit integer. */
231 u_int16_t read_16(FILE *classfile)
234 b1 = fgetc(classfile);
237 b2 = fgetc(classfile);
240 return (u_int16_t)((b1 << 8) | b2);
243 /* Reads in a value from the constant pool. */
244 void skip_constant(FILE *classfile, u_int16_t *cur)
248 pool[*cur] = ftell(classfile);
249 switch(read_8(classfile))
252 len = read_16(classfile);
253 seekerr = fseek(classfile, len, SEEK_CUR);
258 seekerr = fseek(classfile, 2, SEEK_CUR);
260 case CP_METHODHANDLE:
261 seekerr = fseek(classfile, 3, SEEK_CUR);
267 case CP_INTERFACEMETHODREF:
269 case CP_INVOKEDYNAMIC:
270 seekerr = fseek(classfile, 4, SEEK_CUR);
274 seekerr = fseek(classfile, 8, SEEK_CUR);
284 void error(const char *format, ...)
287 va_start(ap, format);
288 vfprintf(stderr, format, ap);
293 int main(int argc, char **argv)
296 u_int16_t cp_count, i, this_class, classinfo_ptr;
302 error("%s: Missing input file\n", program);
303 classfile = fopen(argv[1], "rb");
305 error("%s: Error opening %s\n", program, argv[1]);
307 if(fseek(classfile, 8, SEEK_SET)) /* skip magic and version numbers */
309 cp_count = read_16(classfile);
310 pool = calloc(cp_count, sizeof(long));
312 error("%s: Out of memory for constant pool\n", program);
314 for(i = 1; i < cp_count; ++i)
315 skip_constant(classfile, &i);
316 if(fseek(classfile, 2, SEEK_CUR)) /* skip access flags */
319 this_class = read_16(classfile);
320 if(this_class < 1 || this_class >= cp_count)
322 if(!pool[this_class] || pool[this_class] == -1)
324 if(fseek(classfile, pool[this_class] + 1, SEEK_SET))
327 classinfo_ptr = read_16(classfile);
328 if(classinfo_ptr < 1 || classinfo_ptr >= cp_count)
330 if(!pool[classinfo_ptr] || pool[classinfo_ptr] == -1)
332 if(fseek(classfile, pool[classinfo_ptr] + 1, SEEK_SET))
335 length = read_16(classfile);
336 for(i = 0; i < length; ++i)
338 u_int8_t x = read_8(classfile);
341 if((x & 0xE0) == 0xC0)
343 u_int8_t y = read_8(classfile);
344 if((y & 0xC0) == 0x80)
346 int c = ((x & 0x1f) << 6) + (y & 0x3f);
354 else if(x == '/') putchar('.');
366 # /usr/local/java/bin/jarwrapper - the wrapper for binfmt_misc/jar
371 Now simply ``chmod +x`` the ``.class``, ``.jar`` and/or ``.html`` files you
374 To add a Java program to your path best put a symbolic link to the main
375 .class file into /usr/bin (or another place you like) omitting the .class
376 extension. The directory containing the original .class file will be
377 added to your CLASSPATH during execution.
380 To test your new setup, enter in the following simple Java app, and name
381 it "HelloWorld.java"::
384 public static void main(String args[]) {
385 System.out.println("Hello World!");
389 Now compile the application with::
391 javac HelloWorld.java
393 Set the executable permissions of the binary file, with::
395 chmod 755 HelloWorld.class
397 And then execute it::
402 To execute Java Jar files, simple chmod the ``*.jar`` files to include
403 the execution bit, then just do::
408 To execute Java Applets, simple chmod the ``*.html`` files to include
409 the execution bit, then just do::
414 originally by Brian A. Lantz, brian@lantz.com
415 heavily edited for binfmt_misc by Richard Günther
416 new scripts by Colin J. Watson <cjw44@cam.ac.uk>
417 added executable Jar file support by Kurt Huwig <kurt@iku-netz.de>