java.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 cause you have to fix the path names. Given path/file names 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. ====================== Cut here ===================
  48. #!/bin/bash
  49. # /usr/local/bin/javawrapper - the wrapper for binfmt_misc/java
  50. if [ -z "$1" ]; then
  51. exec 1>&2
  52. echo Usage: $0 class-file
  53. exit 1
  54. fi
  55. CLASS=$1
  56. FQCLASS=`/usr/local/bin/javaclassname $1`
  57. FQCLASSN=`echo $FQCLASS | sed -e 's/^.*\.\([^.]*\)$/\1/'`
  58. FQCLASSP=`echo $FQCLASS | sed -e 's-\.-/-g' -e 's-^[^/]*$--' -e 's-/[^/]*$--'`
  59. # for example:
  60. # CLASS=Test.class
  61. # FQCLASS=foo.bar.Test
  62. # FQCLASSN=Test
  63. # FQCLASSP=foo/bar
  64. unset CLASSBASE
  65. declare -i LINKLEVEL=0
  66. while :; do
  67. if [ "`basename $CLASS .class`" == "$FQCLASSN" ]; then
  68. # See if this directory works straight off
  69. cd -L `dirname $CLASS`
  70. CLASSDIR=$PWD
  71. cd $OLDPWD
  72. if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
  73. CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
  74. break;
  75. fi
  76. # Try dereferencing the directory name
  77. cd -P `dirname $CLASS`
  78. CLASSDIR=$PWD
  79. cd $OLDPWD
  80. if echo $CLASSDIR | grep -q "$FQCLASSP$"; then
  81. CLASSBASE=`echo $CLASSDIR | sed -e "s.$FQCLASSP$.."`
  82. break;
  83. fi
  84. # If no other possible filename exists
  85. if [ ! -L $CLASS ]; then
  86. exec 1>&2
  87. echo $0:
  88. echo " $CLASS should be in a" \
  89. "directory tree called $FQCLASSP"
  90. exit 1
  91. fi
  92. fi
  93. if [ ! -L $CLASS ]; then break; fi
  94. # Go down one more level of symbolic links
  95. let LINKLEVEL+=1
  96. if [ $LINKLEVEL -gt 5 ]; then
  97. exec 1>&2
  98. echo $0:
  99. echo " Too many symbolic links encountered"
  100. exit 1
  101. fi
  102. CLASS=`ls --color=no -l $CLASS | sed -e 's/^.* \([^ ]*\)$/\1/'`
  103. done
  104. if [ -z "$CLASSBASE" ]; then
  105. if [ -z "$FQCLASSP" ]; then
  106. GOODNAME=$FQCLASSN.class
  107. else
  108. GOODNAME=$FQCLASSP/$FQCLASSN.class
  109. fi
  110. exec 1>&2
  111. echo $0:
  112. echo " $FQCLASS should be in a file called $GOODNAME"
  113. exit 1
  114. fi
  115. if ! echo $CLASSPATH | grep -q "^\(.*:\)*$CLASSBASE\(:.*\)*"; then
  116. # class is not in CLASSPATH, so prepend dir of class to CLASSPATH
  117. if [ -z "${CLASSPATH}" ] ; then
  118. export CLASSPATH=$CLASSBASE
  119. else
  120. export CLASSPATH=$CLASSBASE:$CLASSPATH
  121. fi
  122. fi
  123. shift
  124. /usr/bin/java $FQCLASS "$@"
  125. ====================== Cut here ===================
  126. ====================== Cut here ===================
  127. /* javaclassname.c
  128. *
  129. * Extracts the class name from a Java class file; intended for use in a Java
  130. * wrapper of the type supported by the binfmt_misc option in the Linux kernel.
  131. *
  132. * Copyright (C) 1999 Colin J. Watson <cjw44@cam.ac.uk>.
  133. *
  134. * This program is free software; you can redistribute it and/or modify
  135. * it under the terms of the GNU General Public License as published by
  136. * the Free Software Foundation; either version 2 of the License, or
  137. * (at your option) any later version.
  138. *
  139. * This program is distributed in the hope that it will be useful,
  140. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  141. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  142. * GNU General Public License for more details.
  143. *
  144. * You should have received a copy of the GNU General Public License
  145. * along with this program; if not, write to the Free Software
  146. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  147. */
  148. #include <stdlib.h>
  149. #include <stdio.h>
  150. #include <stdarg.h>
  151. #include <sys/types.h>
  152. /* From Sun's Java VM Specification, as tag entries in the constant pool. */
  153. #define CP_UTF8 1
  154. #define CP_INTEGER 3
  155. #define CP_FLOAT 4
  156. #define CP_LONG 5
  157. #define CP_DOUBLE 6
  158. #define CP_CLASS 7
  159. #define CP_STRING 8
  160. #define CP_FIELDREF 9
  161. #define CP_METHODREF 10
  162. #define CP_INTERFACEMETHODREF 11
  163. #define CP_NAMEANDTYPE 12
  164. /* Define some commonly used error messages */
  165. #define seek_error() error("%s: Cannot seek\n", program)
  166. #define corrupt_error() error("%s: Class file corrupt\n", program)
  167. #define eof_error() error("%s: Unexpected end of file\n", program)
  168. #define utf8_error() error("%s: Only ASCII 1-255 supported\n", program);
  169. char *program;
  170. long *pool;
  171. u_int8_t read_8(FILE *classfile);
  172. u_int16_t read_16(FILE *classfile);
  173. void skip_constant(FILE *classfile, u_int16_t *cur);
  174. void error(const char *format, ...);
  175. int main(int argc, char **argv);
  176. /* Reads in an unsigned 8-bit integer. */
  177. u_int8_t read_8(FILE *classfile)
  178. {
  179. int b = fgetc(classfile);
  180. if(b == EOF)
  181. eof_error();
  182. return (u_int8_t)b;
  183. }
  184. /* Reads in an unsigned 16-bit integer. */
  185. u_int16_t read_16(FILE *classfile)
  186. {
  187. int b1, b2;
  188. b1 = fgetc(classfile);
  189. if(b1 == EOF)
  190. eof_error();
  191. b2 = fgetc(classfile);
  192. if(b2 == EOF)
  193. eof_error();
  194. return (u_int16_t)((b1 << 8) | b2);
  195. }
  196. /* Reads in a value from the constant pool. */
  197. void skip_constant(FILE *classfile, u_int16_t *cur)
  198. {
  199. u_int16_t len;
  200. int seekerr = 1;
  201. pool[*cur] = ftell(classfile);
  202. switch(read_8(classfile))
  203. {
  204. case CP_UTF8:
  205. len = read_16(classfile);
  206. seekerr = fseek(classfile, len, SEEK_CUR);
  207. break;
  208. case CP_CLASS:
  209. case CP_STRING:
  210. seekerr = fseek(classfile, 2, SEEK_CUR);
  211. break;
  212. case CP_INTEGER:
  213. case CP_FLOAT:
  214. case CP_FIELDREF:
  215. case CP_METHODREF:
  216. case CP_INTERFACEMETHODREF:
  217. case CP_NAMEANDTYPE:
  218. seekerr = fseek(classfile, 4, SEEK_CUR);
  219. break;
  220. case CP_LONG:
  221. case CP_DOUBLE:
  222. seekerr = fseek(classfile, 8, SEEK_CUR);
  223. ++(*cur);
  224. break;
  225. default:
  226. corrupt_error();
  227. }
  228. if(seekerr)
  229. seek_error();
  230. }
  231. void error(const char *format, ...)
  232. {
  233. va_list ap;
  234. va_start(ap, format);
  235. vfprintf(stderr, format, ap);
  236. va_end(ap);
  237. exit(1);
  238. }
  239. int main(int argc, char **argv)
  240. {
  241. FILE *classfile;
  242. u_int16_t cp_count, i, this_class, classinfo_ptr;
  243. u_int8_t length;
  244. program = argv[0];
  245. if(!argv[1])
  246. error("%s: Missing input file\n", program);
  247. classfile = fopen(argv[1], "rb");
  248. if(!classfile)
  249. error("%s: Error opening %s\n", program, argv[1]);
  250. if(fseek(classfile, 8, SEEK_SET)) /* skip magic and version numbers */
  251. seek_error();
  252. cp_count = read_16(classfile);
  253. pool = calloc(cp_count, sizeof(long));
  254. if(!pool)
  255. error("%s: Out of memory for constant pool\n", program);
  256. for(i = 1; i < cp_count; ++i)
  257. skip_constant(classfile, &i);
  258. if(fseek(classfile, 2, SEEK_CUR)) /* skip access flags */
  259. seek_error();
  260. this_class = read_16(classfile);
  261. if(this_class < 1 || this_class >= cp_count)
  262. corrupt_error();
  263. if(!pool[this_class] || pool[this_class] == -1)
  264. corrupt_error();
  265. if(fseek(classfile, pool[this_class] + 1, SEEK_SET))
  266. seek_error();
  267. classinfo_ptr = read_16(classfile);
  268. if(classinfo_ptr < 1 || classinfo_ptr >= cp_count)
  269. corrupt_error();
  270. if(!pool[classinfo_ptr] || pool[classinfo_ptr] == -1)
  271. corrupt_error();
  272. if(fseek(classfile, pool[classinfo_ptr] + 1, SEEK_SET))
  273. seek_error();
  274. length = read_16(classfile);
  275. for(i = 0; i < length; ++i)
  276. {
  277. u_int8_t x = read_8(classfile);
  278. if((x & 0x80) || !x)
  279. {
  280. if((x & 0xE0) == 0xC0)
  281. {
  282. u_int8_t y = read_8(classfile);
  283. if((y & 0xC0) == 0x80)
  284. {
  285. int c = ((x & 0x1f) << 6) + (y & 0x3f);
  286. if(c) putchar(c);
  287. else utf8_error();
  288. }
  289. else utf8_error();
  290. }
  291. else utf8_error();
  292. }
  293. else if(x == '/') putchar('.');
  294. else putchar(x);
  295. }
  296. putchar('\n');
  297. free(pool);
  298. fclose(classfile);
  299. return 0;
  300. }
  301. ====================== Cut here ===================
  302. ====================== Cut here ===================
  303. #!/bin/bash
  304. # /usr/local/java/bin/jarwrapper - the wrapper for binfmt_misc/jar
  305. java -jar $1
  306. ====================== Cut here ===================
  307. Now simply chmod +x the .class, .jar and/or .html files you want to execute.
  308. To add a Java program to your path best put a symbolic link to the main
  309. .class file into /usr/bin (or another place you like) omitting the .class
  310. extension. The directory containing the original .class file will be
  311. added to your CLASSPATH during execution.
  312. To test your new setup, enter in the following simple Java app, and name
  313. it "HelloWorld.java":
  314. class HelloWorld {
  315. public static void main(String args[]) {
  316. System.out.println("Hello World!");
  317. }
  318. }
  319. Now compile the application with:
  320. javac HelloWorld.java
  321. Set the executable permissions of the binary file, with:
  322. chmod 755 HelloWorld.class
  323. And then execute it:
  324. ./HelloWorld.class
  325. To execute Java Jar files, simple chmod the *.jar files to include
  326. the execution bit, then just do
  327. ./Application.jar
  328. To execute Java Applets, simple chmod the *.html files to include
  329. the execution bit, then just do
  330. ./Applet.html
  331. originally by Brian A. Lantz, brian@lantz.com
  332. heavily edited for binfmt_misc by Richard Günther
  333. new scripts by Colin J. Watson <cjw44@cam.ac.uk>
  334. added executable Jar file support by Kurt Huwig <kurt@iku-netz.de>