0

So I made a previous post on how to compile and run a java file But I came to a conclusion of a better idea:- for example I have this java code

public class Main {
public static void main(String[] args){
    String test = 123;
    Scanner s = new Scanner(System.in);
    String code = s.nextLine();
    // Code Here
}

So I want the code to work like this if what I type in my String (code) is System.out.println(test + " 123"); it prints out (123 123) So what is the code that I should replace in // Code Here Please make sure that all the variables and libraries in this project are compiled within the code, and please make the answer simple please, the javacompiler class explanation in oracle is kind of hard to comprehend because can you please post an answer in here!

hamoodrex
  • 35
  • 7
  • Basically, if you need this, you need to think carefully whether you have chosen the right language (Java) for your purpose. Java is a statically typed, compiled language. Yes it is possible to do what you want, but it will not look like your other code, will be several orders of magnitude slower (to compile on the fly) than your pre-compiled code, and introduces all kinds of security-related questions. It's probably better to go with a scripting language if you need this. – Erwin Bolwidt Nov 07 '15 at 11:32
  • @Erwin or both, java can use its scripting engine to run (for example) javascript for the user created stuff and still use java for the rest – Richard Tingle Nov 07 '15 at 12:28

0 Answers0