You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Examples.md 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. How to run program examples
  2. ---------------------------
  3. JDK 1.4 or later is needed.
  4. ### 0. If you have Apache Ant
  5. Run the sample-all task.
  6. Otherwise, follow the instructions below.
  7. ### 1. Move to the directory where this `Examples.md` file is located.
  8. In the following instructions, we assume that the `javassist.jar`
  9. file is included in the class path.
  10. For example, the javac and java commands must receive
  11. the following `classpath` option:
  12. ```
  13. -classpath ".:javassist.jar"
  14. ```
  15. If the operating system is Windows, the path
  16. separator must be not `:` (colon) but
  17. `;` (semicolon). The java command can receive
  18. the `-cp` option
  19. as well as `-classpath`.
  20. If you don't want to use the class-path option, you can make
  21. `javassist.jar` included in the `CLASSPATH`
  22. environment:
  23. ```
  24. export CLASSPATH=.:javassist.jar
  25. ```
  26. or if the operating system is Windows:
  27. ```
  28. set CLASSPATH=.;javassist.jar
  29. ```
  30. Otherwise, you can copy `javassist.jar` to the directory
  31. <_java-home_>`/jre/lib/ext`.
  32. <_java-home_> depends on the system. It is usually
  33. `/usr/local/java` or `c:\j2sdk1.4\`, etc.
  34. ### 2. `sample/Test.java`
  35. This is a very simple program using Javassist.
  36. To run, type the commands:
  37. ```
  38. % javac sample/Test.java
  39. % java sample.Test
  40. ```
  41. For more details, see `sample/Test.java`
  42. ### 3. `sample/reflect/*.java`
  43. This is the "verbose metaobject" example well known in reflective
  44. programming. This program dynamically attaches a metaobject to
  45. a Person object. The metaobject prints a message if a method
  46. is called on the Person object.
  47. To run, type the commands:
  48. ```
  49. % javac sample/reflect/*.java
  50. % java javassist.tools.reflect.Loader sample.reflect.Main Joe
  51. ```
  52. Compare this result with that of the regular execution without reflection:
  53. ```
  54. % java sample.reflect.Person Joe
  55. ```
  56. For more details, see sample/reflect/Main.java
  57. Furthermore, the Person class can be statically modified so that
  58. all the Person objects become reflective without sample.reflect.Main.
  59. To do this, type the commands:
  60. ```
  61. % java javassist.tools.reflect.Compiler sample.reflect.Person -m sample.reflect.VerboseMetaobj
  62. ```
  63. Then,
  64. ```
  65. % java sample.reflect.Person Joe
  66. ```
  67. ### 4. `sample/duplicate/*.java`
  68. This is another example of reflective programming.
  69. To run, type the commands:
  70. ```
  71. % javac sample/duplicate/*.java
  72. % java sample.duplicate.Main
  73. ```
  74. Compare this result with that of the regular execution without reflection:
  75. ```
  76. % java sample.duplicate.Viewer
  77. ```
  78. For more details, see
  79. `sample/duplicate/Main.java`
  80. ### 5. `sample/vector/*.java`
  81. This example shows the use of Javassit for producing a class representing
  82. a vector of a given type at compile time.
  83. To run, type the commands:
  84. ```
  85. % javac sample/vector/*.java
  86. % java sample.preproc.Compiler sample/vector/Test.j
  87. % javac sample/vector/Test.java
  88. % java sample.vector.Test
  89. ```
  90. Note: `javassist.jar` is unnecessary to compile and execute
  91. `sample/vector/Test.java`.
  92. For more details, see
  93. `sample/vector/Test.j` and `sample/vector/VectorAssistant.java`.
  94. ### 6. `sample/rmi/*.java`
  95. This demonstrates the `javassist.rmi` package.
  96. To run, type the commands:
  97. ```
  98. % javac sample/rmi/*.java
  99. % java sample.rmi.Counter 5001
  100. ```
  101. The second line starts a web server listening to port 5001.
  102. Then, open sample/rmi/webdemo.html
  103. with a web browser running
  104. on the local host. (`webdemo.html` trys to fetch an applet from
  105. `http://localhost:5001/`, which is the web server we started above.)
  106. Otherwise, run `sample.rmi.CountApplet` as an application:
  107. ```
  108. % java javassist.web.Viewer localhost 5001 sample.rmi.CountApplet
  109. ```
  110. ### 7. `sample/evolve/*.java`
  111. This is a demonstration of the class evolution mechanism implemented
  112. with Javassist. This mechanism enables a Java program to reload an
  113. existing class file under some restriction.
  114. To run, type the commands:
  115. ```
  116. % javac sample/evolve/*.java
  117. % java sample.evolve.DemoLoader 5003
  118. ```
  119. The second line starts a class loader DemoLoader, which runs a web
  120. server DemoServer listening to port 5003.
  121. Then, open `http://localhost:5003/demo.html` with a web browser running
  122. on the local host.
  123. (Or, see sample/evolve/start.html.)
  124. ### 8. `sample/hotswap/*.java`
  125. This shows dynamic class reloading by the JPDA. It needs JDK 1.4 or later.
  126. To run, first type the following commands:
  127. ```
  128. % cd sample/hotswap
  129. % javac *.java
  130. % cd logging
  131. % javac *.java
  132. % cd ..
  133. ```
  134. If your Java is 1.4, then type:
  135. ```
  136. % java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 Test
  137. ```
  138. If you are using Java 5, then type:
  139. ```
  140. % java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 Test
  141. ```
  142. Note that the class path must include `JAVA_HOME/lib/tools.jar`.