/docs/heclandjava.html

https://github.com/soundanny/hecl · HTML · 60 lines · 42 code · 18 blank · 0 comment · 0 complexity · 9562f03111aeb6d6fcc0bbe8938dd8bb MD5 · raw file

  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Interfacing Hecl and Java</title><link rel="stylesheet" href="hecl.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.1"><link rel="home" href="index.html" title="Hecl - The Mobile Scripting Language"><link rel="up" href="index.html" title="Hecl - The Mobile Scripting Language"><link rel="prev" href="kxml.getname.html" title="kxml.getname"><link rel="next" href="creating_new_hecl_commands.html" title="Creating new Hecl commands"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Interfacing Hecl and Java</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="kxml.getname.html"><img src="images/prev.png" alt="Prev"></a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="creating_new_hecl_commands.html"><img src="images/next.png" alt="Next"></a></td></tr></table></div><div class="section" title="Interfacing Hecl and Java"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="heclandjava"></a>Interfacing Hecl and Java</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="heclandjava.html#%0A%09id-1.7.3">Calling Hecl code from Java</a></span></dt><dt><span class="section"><a href="creating_new_hecl_commands.html">Creating new Hecl commands</a></span></dt><dt><span class="section"><a href="building_hecl.html">Building Hecl: Ant Targets</a></span></dt><dt><span class="section"><a href="hecl_javadocs.html">JavaDocs</a></span></dt></dl></div>
  2. <p>
  3. Hecl is not a replacement for Java, and is indeed meant to work hand in hand with Java. We
  4. attempt to make it as easy as possible to call Java from Hecl, via the creation of new Hecl
  5. commands that can call Java code, in addition to calling Hecl from Java, which is a matter of
  6. a few lines of code.
  7. </p>
  8. <div class="section" title="Calling Hecl code from Java"><div class="titlepage"><div><div><h3 class="title"><a name="id-1.7.3.1"></a>Calling Hecl code from Java</h3></div></div></div>
  9. <pre class="programlisting">
  10. import org.hecl.files.HeclFile;
  11. import org.hecl.Eval;
  12. import org.hecl.Interp;
  13. import org.hecl.Thing;
  14. import org.hecl.ListThing;
  15. import org.hecl.HeclException;
  16. ...
  17. try {
  18. /* First, create a new interpreter, and pass it a
  19. * mechanism to load resources with - in this case,
  20. * files. */
  21. Interp interp = new Interp();
  22. /* Add the files package */
  23. new HeclFile().loadModule(interp);
  24. /* Evaluate the file at args[0] */
  25. HeclFile.sourceFile(interp, args[0]);
  26. /* Evaluate some code in a string. */
  27. String helloworld = new String("puts {Hello, world!}");
  28. interp.eval(new Thing(helloworld));
  29. } catch (Exception e) {
  30. System.err.println(e);
  31. }
  32. </pre>
  33. <p>
  34. The above code first creates a new interpreter. Next, it
  35. instantiates the HeclFile system. This isn't part of the Hecl
  36. core, because some systems, like J2ME, may not have files. If
  37. you don't have files, you can still use
  38. <code class="methodname">interp.eval</code> to evaluate some code,
  39. which could come from whatever source you desire.
  40. </p>
  41. </div>
  42. </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="kxml.getname.html"><img src="images/prev.png" alt="Prev"></a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="creating_new_hecl_commands.html"><img src="images/next.png" alt="Next"></a></td></tr><tr><td width="40%" align="left" valign="top">kxml.getname </td><td width="20%" align="center"><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a></td><td width="40%" align="right" valign="top"> Creating new Hecl commands</td></tr></table></div></body></html>