Results 1 to 6 of 6
  1. #1
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Question generate class file during runtime?

    Hi, I wrote some classes that one can use to display functions, polars, parametrics, data, etc...I represent each function with a class that implements a Function interface.

    Java Code:
    public interface Function{
         //returns the value of the function at this input (returns f(x))
         public double at(double x);
    }
    using anonymous classes and eclipse, it's pretty easy to generate graphs by editing a driver class in the editor window. But now I want to create a GUI to make it more user friendly...the way I was thinking of allowing user-defined functions was to have a text box where the user types the return statement for the at method in the Function interface, and then having java compile it into a new function to load. The user would have to use java's syntax, but it seems a helluva lot easier than writing a parser. My only problem is that, from what I understand, I can't use the compiler unless the user has the JDK installed, not just the JVM, like most non-programmers do. My question is if anyone knows how to take simple java code (for example, a return statement like "return x*x" to generate a function that represents y=x^2) generated during run time and load it as if it was written before, by the programmer. I can't thank you guys enough!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Since Java 1.5 the JRE comes bundled with a Javascript engine (written in Java); if you're willing to use Javascript syntax instead (it's very similar to Java's syntax) you can use a Javscript engine. Here's a quick and dirty example:

    Java Code:
    import javax.script.Invocable;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
     
    public class ScriptDemo {
     
        public static void main(String[] args) {
        	
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            
            try {
                String expression = "function add(x, y) { return x+y; }";
                engine.eval(expression);
                
            	engine.put("a", 41);
            	
            	// 
                engine.put("b", 1);
    
                Invocable invocable= (Invocable)engine;
                
                double d= (Double)engine.eval("add(a, b)");
                
                System.out.println(d);
                
                System.out.println("add(1, 41)= "+invocable.invokeFunction("add", 1, 41));
                
                System.out.println("add(a, b)= "+invocable.invokeFunction("add", "a", "b"));
                
                System.out.println("add(a, b)= "+engine.eval("add(a, b)"));
            } catch(ScriptException se) {
                se.printStackTrace();
            } catch (NoSuchMethodException nsme) {
            	nsme.printStackTrace();
            }
    
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Another, Windows-only way is to write out a VBS script to a file, execute it, and collect the return value.

    A couple of examples of this approach:
    Printing excel document without opening
    Find windows special folders (Java in General forum at JavaRanch)

    db

  4. #4
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    After looking into the JavaScript engine, I think I'm going to go with that one, mainly because I understand the procedure better lol. Do you guys think the fact that the functions will be scripted as opposed to java-coded will reduce the speed of execution? the function is evaluated once per horizontal pixel, so 500 times on a 500x500 pane. Thanks so much!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by balla View Post
    After looking into the JavaScript engine, I think I'm going to go with that one, mainly because I understand the procedure better lol. Do you guys think the fact that the functions will be scripted as opposed to java-coded will reduce the speed of execution? the function is evaluated once per horizontal pixel, so 500 times on a 500x500 pane. Thanks so much!
    Of course a purely interpreted language is slower than compiled Java but read the API documentation for the ScriptEngine framework; my example also shows it: an Invocable object only needs to compile the script once and can be run many times; this speeds up the entire thing. 500 expression evaluations (as in your example) doesn't take much time. Another option is BeanShell, it's an interpreted version of a form of Java and it can do great stuff with ordinary compiled Java too.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Can I do something like a static import in JavaScript so that the user can simply write "sin(5)" instead of "Math.sin(5)"? I can't seem to find documentation on it, if it's possible at all.

Similar Threads

  1. Replies: 2
    Last Post: 02-02-2010, 03:01 PM
  2. Generate a Class file
    By Daniel in forum New To Java
    Replies: 6
    Last Post: 04-20-2009, 05:37 AM
  3. Can we generate an array for class object?
    By gaurav2211 in forum Advanced Java
    Replies: 4
    Last Post: 03-21-2009, 04:20 AM
  4. Example of RunTime class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  5. Exception Failed to Generate Wrapper Class on WebLogic
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •