1

I am writing a command-line matrix manipulation tool in Java, and was wondering if it is possible to run Java statements through console input.

I was thinking of using the java.util.Scanner object since that's what I'm using for the rest of my application, but I am open to any solution.

Here is a copy of my tool's application class, so you can see what I mean:

package projects.matrix.main;
import static java.lang.System.*;
import java.util.Scanner;
import projects.matrix.util.MatrixTool;
/**
 * MATRIX :: Application class for the matrix toolset. 
 * @author  toner
 * @version May 28 2015
 * @since   1.8
 **/
public class MarixApp {
    public static void main (String [] args) {
    out.println("****************************** START*****************"  
     + "*************\n");
        runCommandLine();
        out.println("******************************  END  *****************" 
         + "*************\n");
    }

    /**
     * MATRIX.MAIN :: runCommandLine runs a loop command line 
     * @param   none
     * @return  none
     **/
    public static void runCommandLine () {
        // method vars
        Scanner scanner = new Scanner(in);
        MatrixTool matrixtool = new MatrixTool();
        int[][] matrix1 = new int[0][0];
        int[][] matrix2 = new int[0][0];
        int[][] resultmatrix = new int[0][0];
        String command = "";
        int executerret = 0;

        // welcome prints
        out.println("[!] welcome to toner's matrix tool command-line");
        out.println("[!] enter 'HELP' to view available commands\n");

        // commmand-line loop
        do {
            out.print(" [?] >> ");
            command = scanner.nextLine();
            executerret = executecmd(command);
        } while (executerret != -1);
    }

    /**
     * MATRIX.MAIN :: executecmd executes the command passed by runCommandLine
     * @param   cmd         : String
     * @return  returncode  : int
     **/
    public static int executecmd (String cmd) {
        // method vars
        Scanner scanner = new Scanner(in);
        MatrixTool matrixtool = new MatrixTool();
        int returncode = 0;

        // command executer
        switch (cmd) {
            case "HELP" : 
            case "help" : 
                out.println("\n"
                    + "  [%]    ADD        DIVIDE     HELP       "
                    + "MULTIPLY   PRINT"
                    + "  [%]    RUNJAVA    SUBSTRACT  SETMTRX    "
                    + "SETMTRX1   SETMTRX2"
                    + "  [%]    TRANSPOSE  RUNOPS     RESET      "
                    + "EXIT\n");
                break;
            // rest of commands go here
        }
    }
}

Regards.

5
  • what is the problem? Commented May 29, 2015 at 15:02
  • @singhakash No problem yet, I'm want to run console input as a Java statement and I don't know how or where to start. Commented May 29, 2015 at 15:04
  • So you basically want to build a Java interpreter, right? I think it would be much easier to use Groovy (as 'defectus' has suggested) or another programming language more suited for this. Python maybe. Commented May 29, 2015 at 15:16
  • Just a simple advice, instead of providing Scanner ( a wrapper class ) with bytestream, try to implement Scanner so as to provide input in the form of a Buffer, by using something like Scanner scanner = new Scanner ( new BufferedReader ( new InputStreamReader ( System.in ) ) ) Commented May 29, 2015 at 15:28
  • What does that change? Can you explain the advantage of doing it your way instead of using the wrapper class? Commented May 29, 2015 at 17:36

2 Answers 2

2

Have you thought about using Groovy? It's a JVM based dynamic language which means you can dynamically execute code - code that comes as a String. Syntactically it's very close to Java. An example code could look like:

new GroovyShell().evaluate("println 'hello'")
Sign up to request clarification or add additional context in comments.

1 Comment

Looks interesting, I'll give it a shot.
1

If by running console input as java statements, you mean taking the exact input from the console and run it inside your application, you need to compile and run them. You can do so using the Compiler API

1 Comment

how exactly would I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.