blob: 8744e272e6f8b0bbfb1cdb06c30a60407f68fc02 [file] [log] [blame]
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -03001Java(tm) Binary Kernel Support for Linux v1.03
2----------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
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
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030022 the kernel (``CONFIG_BINFMT_MISC``) and set it up properly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 If you choose to compile it as a module, you will have
24 to insert it manually with modprobe/insmod, as kmod
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030025 cannot easily be supported with binfmt_misc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030030 (you should really have read ``binfmt_misc.txt`` now):
31 support for Java applications::
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:'
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030034
35 support for executable Jar files::
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 ':ExecutableJAR:E::jar::/usr/local/bin/jarwrapper:'
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030038
39 support for Java Applets::
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 ':Applet:E::html::/usr/bin/appletviewer:'
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030042
43 or the following, if you want to be more selective::
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ':Applet:M::<!--applet::/usr/bin/appletviewer:'
46
Shaun Zinck59dd24d2007-10-20 02:37:21 +020047 Of course you have to fix the path names. The path/file names given in this
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030048 document match the Debian 2.1 system. (i.e. jdk installed in ``/usr``,
49 custom wrappers from this document in ``/usr/local``)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 Note, that for the more selective applet support you have to modify
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030052 existing html-files to contain ``<!--applet-->`` in the first line
53 (``<`` has to be the first character!) to let this work!
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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.
59
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030060 You, too, need the little program after the script. Compile like::
61
62 gcc -O2 -o javaclassname javaclassname.c
63
64 and stick it to ``/usr/local/bin``.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 Both the javawrapper shellscript and the javaclassname program
67 were supplied by Colin J. Watson <cjw44@cam.ac.uk>.
68
Jani Nikula07a37ba2016-11-03 11:43:29 +020069Javawrapper shell script:
70
71.. code-block:: sh
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030073 #!/bin/bash
74 # /usr/local/bin/javawrapper - the wrapper for binfmt_misc/java
75
76 if [ -z "$1" ]; then
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 exec 1>&2
78 echo Usage: $0 class-file
79 exit 1
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030080 fi
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030082 CLASS=$1
83 FQCLASS=`/usr/local/bin/javaclassname $1`
84 FQCLASSN=`echo $FQCLASS | sed -e 's/^.*\.\([^.]*\)$/\1/'`
85 FQCLASSP=`echo $FQCLASS | sed -e 's-\.-/-g' -e 's-^[^/]*$--' -e 's-/[^/]*$--'`
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030087 # for example:
88 # CLASS=Test.class
89 # FQCLASS=foo.bar.Test
90 # FQCLASSN=Test
91 # FQCLASSP=foo/bar
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030093 unset CLASSBASE
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030095 declare -i LINKLEVEL=0
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -030097 while :; do
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if [ "`basename $CLASS .class`" == "$FQCLASSN" ]; then
99 # See if this directory works straight off
100 cd -L `dirname $CLASS`
101 CLASSDIR=$PWD
102 cd $OLDPWD
103 if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
104 CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
105 break;
106 fi
107 # Try dereferencing the directory name
108 cd -P `dirname $CLASS`
109 CLASSDIR=$PWD
110 cd $OLDPWD
111 if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
112 CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
113 break;
114 fi
115 # If no other possible filename exists
116 if [ ! -L $CLASS ]; then
117 exec 1>&2
118 echo $0:
119 echo " $CLASS should be in a" \
120 "directory tree called $FQCLASSP"
121 exit 1
122 fi
123 fi
124 if [ ! -L $CLASS ]; then break; fi
125 # Go down one more level of symbolic links
126 let LINKLEVEL+=1
127 if [ $LINKLEVEL -gt 5 ]; then
128 exec 1>&2
129 echo $0:
130 echo " Too many symbolic links encountered"
131 exit 1
132 fi
133 CLASS=`ls --color=no -l $CLASS | sed -e 's/^.* \([^ ]*\)$/\1/'`
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300134 done
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300136 if [ -z "$CLASSBASE" ]; then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if [ -z "$FQCLASSP" ]; then
138 GOODNAME=$FQCLASSN.class
139 else
140 GOODNAME=$FQCLASSP/$FQCLASSN.class
141 fi
142 exec 1>&2
143 echo $0:
144 echo " $FQCLASS should be in a file called $GOODNAME"
145 exit 1
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300146 fi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300148 if ! echo $CLASSPATH | grep -q "^\(.*:\)*$CLASSBASE\(:.*\)*"; then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 # class is not in CLASSPATH, so prepend dir of class to CLASSPATH
150 if [ -z "${CLASSPATH}" ] ; then
151 export CLASSPATH=$CLASSBASE
152 else
153 export CLASSPATH=$CLASSBASE:$CLASSPATH
154 fi
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300155 fi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300157 shift
158 /usr/bin/java $FQCLASS "$@"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jani Nikula07a37ba2016-11-03 11:43:29 +0200160javaclassname.c:
161
162.. code-block:: c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300164 /* javaclassname.c
Jani Nikula07a37ba2016-11-03 11:43:29 +0200165 *
166 * Extracts the class name from a Java class file; intended for use in a Java
167 * wrapper of the type supported by the binfmt_misc option in the Linux kernel.
168 *
169 * Copyright (C) 1999 Colin J. Watson <cjw44@cam.ac.uk>.
170 *
171 * This program is free software; you can redistribute it and/or modify
172 * it under the terms of the GNU General Public License as published by
173 * the Free Software Foundation; either version 2 of the License, or
174 * (at your option) any later version.
175 *
176 * This program is distributed in the hope that it will be useful,
177 * but WITHOUT ANY WARRANTY; without even the implied warranty of
178 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
179 * GNU General Public License for more details.
180 *
181 * You should have received a copy of the GNU General Public License
182 * along with this program; if not, write to the Free Software
183 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300186 #include <stdlib.h>
187 #include <stdio.h>
188 #include <stdarg.h>
189 #include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300191 /* From Sun's Java VM Specification, as tag entries in the constant pool. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300193 #define CP_UTF8 1
194 #define CP_INTEGER 3
195 #define CP_FLOAT 4
196 #define CP_LONG 5
197 #define CP_DOUBLE 6
198 #define CP_CLASS 7
199 #define CP_STRING 8
200 #define CP_FIELDREF 9
201 #define CP_METHODREF 10
202 #define CP_INTERFACEMETHODREF 11
203 #define CP_NAMEANDTYPE 12
204 #define CP_METHODHANDLE 15
205 #define CP_METHODTYPE 16
206 #define CP_INVOKEDYNAMIC 18
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300208 /* Define some commonly used error messages */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300210 #define seek_error() error("%s: Cannot seek\n", program)
211 #define corrupt_error() error("%s: Class file corrupt\n", program)
212 #define eof_error() error("%s: Unexpected end of file\n", program)
213 #define utf8_error() error("%s: Only ASCII 1-255 supported\n", program);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300215 char *program;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300217 long *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300219 u_int8_t read_8(FILE *classfile);
220 u_int16_t read_16(FILE *classfile);
221 void skip_constant(FILE *classfile, u_int16_t *cur);
222 void error(const char *format, ...);
223 int main(int argc, char **argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300225 /* Reads in an unsigned 8-bit integer. */
226 u_int8_t read_8(FILE *classfile)
227 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int b = fgetc(classfile);
229 if(b == EOF)
230 eof_error();
231 return (u_int8_t)b;
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300234 /* Reads in an unsigned 16-bit integer. */
235 u_int16_t read_16(FILE *classfile)
236 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int b1, b2;
238 b1 = fgetc(classfile);
239 if(b1 == EOF)
240 eof_error();
241 b2 = fgetc(classfile);
242 if(b2 == EOF)
243 eof_error();
244 return (u_int16_t)((b1 << 8) | b2);
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300247 /* Reads in a value from the constant pool. */
248 void skip_constant(FILE *classfile, u_int16_t *cur)
249 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u_int16_t len;
251 int seekerr = 1;
252 pool[*cur] = ftell(classfile);
253 switch(read_8(classfile))
254 {
255 case CP_UTF8:
256 len = read_16(classfile);
257 seekerr = fseek(classfile, len, SEEK_CUR);
258 break;
259 case CP_CLASS:
260 case CP_STRING:
Jonathan Callenf76f133b2014-05-15 13:54:52 -0700261 case CP_METHODTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 seekerr = fseek(classfile, 2, SEEK_CUR);
263 break;
Jonathan Callenf76f133b2014-05-15 13:54:52 -0700264 case CP_METHODHANDLE:
265 seekerr = fseek(classfile, 3, SEEK_CUR);
266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 case CP_INTEGER:
268 case CP_FLOAT:
269 case CP_FIELDREF:
270 case CP_METHODREF:
271 case CP_INTERFACEMETHODREF:
272 case CP_NAMEANDTYPE:
Jonathan Callenf76f133b2014-05-15 13:54:52 -0700273 case CP_INVOKEDYNAMIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 seekerr = fseek(classfile, 4, SEEK_CUR);
275 break;
276 case CP_LONG:
277 case CP_DOUBLE:
278 seekerr = fseek(classfile, 8, SEEK_CUR);
279 ++(*cur);
280 break;
281 default:
282 corrupt_error();
283 }
284 if(seekerr)
285 seek_error();
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300288 void error(const char *format, ...)
289 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 va_list ap;
291 va_start(ap, format);
292 vfprintf(stderr, format, ap);
293 va_end(ap);
294 exit(1);
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300297 int main(int argc, char **argv)
298 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 FILE *classfile;
300 u_int16_t cp_count, i, this_class, classinfo_ptr;
301 u_int8_t length;
302
303 program = argv[0];
304
305 if(!argv[1])
306 error("%s: Missing input file\n", program);
307 classfile = fopen(argv[1], "rb");
308 if(!classfile)
309 error("%s: Error opening %s\n", program, argv[1]);
310
311 if(fseek(classfile, 8, SEEK_SET)) /* skip magic and version numbers */
312 seek_error();
313 cp_count = read_16(classfile);
314 pool = calloc(cp_count, sizeof(long));
315 if(!pool)
316 error("%s: Out of memory for constant pool\n", program);
317
318 for(i = 1; i < cp_count; ++i)
319 skip_constant(classfile, &i);
320 if(fseek(classfile, 2, SEEK_CUR)) /* skip access flags */
321 seek_error();
322
323 this_class = read_16(classfile);
324 if(this_class < 1 || this_class >= cp_count)
325 corrupt_error();
326 if(!pool[this_class] || pool[this_class] == -1)
327 corrupt_error();
328 if(fseek(classfile, pool[this_class] + 1, SEEK_SET))
329 seek_error();
330
331 classinfo_ptr = read_16(classfile);
332 if(classinfo_ptr < 1 || classinfo_ptr >= cp_count)
333 corrupt_error();
334 if(!pool[classinfo_ptr] || pool[classinfo_ptr] == -1)
335 corrupt_error();
336 if(fseek(classfile, pool[classinfo_ptr] + 1, SEEK_SET))
337 seek_error();
338
339 length = read_16(classfile);
340 for(i = 0; i < length; ++i)
341 {
342 u_int8_t x = read_8(classfile);
343 if((x & 0x80) || !x)
344 {
345 if((x & 0xE0) == 0xC0)
346 {
347 u_int8_t y = read_8(classfile);
348 if((y & 0xC0) == 0x80)
349 {
350 int c = ((x & 0x1f) << 6) + (y & 0x3f);
351 if(c) putchar(c);
352 else utf8_error();
353 }
354 else utf8_error();
355 }
356 else utf8_error();
357 }
358 else if(x == '/') putchar('.');
359 else putchar(x);
360 }
361 putchar('\n');
362 free(pool);
363 fclose(classfile);
364 return 0;
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300365 }
366
367jarwrapper::
368
369 #!/bin/bash
370 # /usr/local/java/bin/jarwrapper - the wrapper for binfmt_misc/jar
371
372 java -jar $1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300375Now simply ``chmod +x`` the ``.class``, ``.jar`` and/or ``.html`` files you
376want to execute.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378To add a Java program to your path best put a symbolic link to the main
379.class file into /usr/bin (or another place you like) omitting the .class
380extension. The directory containing the original .class file will be
381added to your CLASSPATH during execution.
382
383
384To test your new setup, enter in the following simple Java app, and name
Jani Nikula07a37ba2016-11-03 11:43:29 +0200385it "HelloWorld.java":
386
387.. code-block:: java
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 class HelloWorld {
390 public static void main(String args[]) {
391 System.out.println("Hello World!");
392 }
393 }
394
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300395Now compile the application with::
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 javac HelloWorld.java
398
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300399Set the executable permissions of the binary file, with::
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 chmod 755 HelloWorld.class
402
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300403And then execute it::
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ./HelloWorld.class
406
407
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300408To execute Java Jar files, simple chmod the ``*.jar`` files to include
409the execution bit, then just do::
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ./Application.jar
412
413
Mauro Carvalho Chehab503c5bf2016-09-23 14:02:36 -0300414To execute Java Applets, simple chmod the ``*.html`` files to include
415the execution bit, then just do::
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ./Applet.html
418
419
420originally by Brian A. Lantz, brian@lantz.com
John Anthony Kazos Jrbe2a6082007-05-09 08:50:42 +0200421heavily edited for binfmt_misc by Richard GΓΌnther
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422new scripts by Colin J. Watson <cjw44@cam.ac.uk>
423added executable Jar file support by Kurt Huwig <kurt@iku-netz.de>