java.rst 11 KB

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