Results 1 to 3 of 3
  1. #1
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default Java method to run code from string

    I know that this sounds a little weird, but is there anyway to take a string that is a string of java code, and run it. like if I had the string Program and it had the contents:

    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
    
    public static void main(String[] args) {
    
    System.out.println("What is your name?");
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    
    
    
    }
    
    
    
    }
    and then i wanted to run the code within the string, is that possible? If so how would I do that? thanks!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There are lots of scripting languages for Java. If you want the string to be executed as Java code then BeanShell could do what you want.

  3. #3
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Pass String[] into method in different class
    By Swankee in forum New To Java
    Replies: 19
    Last Post: 09-28-2009, 06:30 PM
  2. Invoking method from String
    By Supamagier in forum Advanced Java
    Replies: 12
    Last Post: 05-29-2009, 10:21 AM
  3. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 08:33 AM
  4. Method for String to int conversion
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:35 PM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 09:14 PM

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
  •