XGN-Python/Jython.
java
1 // XGN-Python/[Link]
2
3 import java . i o . BufferedReader ;
4 import j a v a . i o . FileNotFoundException ;
5 import java . io . FileReader ;
6 import j a v a . i o . IOException ;
7 import j a v a . i o . InputStreamReader ;
8 import j a v a . i o . OutputStreamWriter ;
9 import j a v a . i o . PipedInputStream ;
10 import j a v a . i o . PipedOutputStream ;
11 import j a v a . i o . Writer ;
12
13 import javax . script . ScriptContext ;
14 import javax . script . ScriptEngine ;
15 import javax . script . ScriptEngineManager ;
16 import javax . script . ScriptException ;
17
18 // ../lib/[Link] must be
19 // in the classpath when running the program
20
21 p u b l i c c l a s s Jython {
22 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
23 PipedOutputStream pos = new PipedOutputStream ( ) ;
24 PipedInputStream p i s = new PipedInputStream ( ) ;
25 try {
26 p i s . c o n n e c t ( pos ) ; // or [Link](pis);
27 } c a t c h ( IOException exc ) {
28 System . e r r . p r i n t l n ( ” Should n e v e r happen ” ) ;
29 System . e x i t ( 1 ) ;
30 }
31 f i n a l Writer w r i t e r = new OutputStreamWriter ( pos ) ;
32 f i n a l B u f f e r e d R e a d e r br =
33 new B u f f e r e d R e a d e r (
34 new InputStreamReader ( p i s ) ) ;
35
36 f i n a l ScriptEngine engine =
37 new ScriptEngineManager ( ) .
38 getEngineByName ( ” python ” ) ;
39 f i n a l ScriptContext context = engine . getContext ( ) ;
40 context . setWriter ( writer ) ;
41
42 new Thread ( ) {
43 @Override
44 p u b l i c v o i d run ( ) {
45 try {
1
46 engine . eval (
47 new F i l e R e a d e r ( ” pyt . py” ) , c o n t e x t ) ;
48 } catch ( ScriptException |
49 FileNotFoundException e ) { }
50 }
51 }. start ( ) ;
52
53 new Thread ( ) {
54 p u b l i c v o i d run ( ) {
55 String s = null ;
56 try {
57 w h i l e ( ( s = br . r e a d L i n e ( ) ) != n u l l ) {
58 i f ( s . s t a r t s W i t h ( ” bye ” ) ) break ;
59 System . out . p r i n t l n ( ” Java : ” + s ) ;
60 }
61 } c a t c h ( IOException e ) {
62 } finally {
63 System . out . p r i n t l n ( ”BYE” ) ;
64 try { writer . close ( ) ; }
65 c a t c h ( IOException e ) { }
66 t r y { br . c l o s e ( ) ; }
67 c a t c h ( IOException e ) { }
68 }
69 }
70 }. start ( ) ;
71 }
72 }