0

Hi was wondering if there was a way to execute a line of code from a text file through Java.

For example, let's say I have a text file, and inside that text file it contains an expression

int a = 1;
int b = 2;
int c;
c = a + b;

So in my main class, I have a file object and it does a try/catch

File file = new File("test.txt");

try {

    //code here

}catch (FileNotFoundException e) {
    System.err.format("File does not exist \n");
}

My goal is to run the code from the test.txt and output the answer. How should I approach this?

Jake
  • 313
  • 1
  • 7
  • 16
  • is there any particular reason you want to have actual code in the file instead of just having the values and processing them in your code? – chatton Dec 03 '16 at 01:06
  • 2
    This is not a normal or commonplace thing to do. If you're new to programming or java, there's a 100% chance that this is the wrong way to do what you want to do. Having said that, If you want to embed a java compatible scripting engine in a program, there's beanshell. – that other guy Dec 03 '16 at 01:13
  • @chatton It's a extra credit assignment. I know how to output what's within the text file if I just want a String. But because I have to output the result of the expression which is an int or double is kind of throwing me off. – Jake Dec 03 '16 at 01:21
  • 2
    You'd need some form of parsing and semantic analysis to do this. You are asking us how to write an interpreter, and seeing how you haven't even posted your attempt or specified a specific problem you are having when implementing this, this is faaar too broad – Vince Dec 03 '16 at 01:25
  • is there a set format that you know the file will follow? In the example you have the contents of the file declaring variables and doing some addition. Do you know the possible lines of code that the file could have? If it's limited, then it shouldn't be too hard to implement something like this (as someone above said I wouldn't recommend something like this, I find it quite strange that there's extra credit for this!) – chatton Dec 03 '16 at 01:27
  • You might try using the [JavaCompiler](http://stackoverflow.com/a/6052010/3284624) tool, however, it requires the JDK so if you're turning it in for extra credit make sure your instructor will run it using the JDK not the JRE. – D.B. Dec 03 '16 at 03:20
  • This is not easily doable in Java. I think [this question](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) will further help you on your way. – PostLee Dec 03 '16 at 01:22

1 Answers1

0

Firstly as from the @D.B. commentary:

You might try using the JavaCompiler tool, however, it requires the JDK so if you're turning it in for extra credit make sure your instructor will run it using the JDK not the JRE

It would be possible to compile the while while you run your program. It would be something like:

  1. Read the file.
  2. Compose a valid Java application.
  3. Compile it.
  4. And start it to run on a new thread.

Now, basically you cannot write Java Standard Edition expressions on a file and load the to execute on run time. It is because all java statements must to be parsed and translated to Java Bytecodes and it can only be done before the program to starts running. Except the case where you construct another Java application, compile it and put it to run as a new application invocation, done by you or some Java System Call.

The only thing the Java Virtual Machine can run are Java Bytecodes. So, your text file is not translated to it, then they cannot run. Although, this is not true to languages as Python and Javascript due they do not need to be translated into Machine Language as Bytecodes for the Java virtual Machine and assembly code for the x86 or x64 processor architectures as languages as C++.

You solution is use a interpreted language as Python and Javascript, instead of a compiled languages as Java or C++. As pointed by @that other guy there is the scripting language BeanShell which is a Java-like scripting language, if you are interested to learn it.

References:

  1. https://en.wikipedia.org/wiki/Interpreted_language
  2. https://en.wikipedia.org/wiki/Compiled_language
  3. https://en.wikipedia.org/wiki/Scripting_language

My goal is to run the code from the test.txt and output the answer. How should I approach this?

Within Java, to achieve this goal you can parse the file using your own syntax rules. And as well stated by @Vince Emigh

You'd need some form of parsing and semantic analysis to do this. You are asking us how to write an interpreter, and seeing how you haven't even posted your attempt or specified a specific problem you are having when implementing this, this is faaar too broad

For your example, it would be like:

text.txt

1
2
+

Your Java Source Code file:

File file = new File("test.txt");

try
{
    BufferedReader br        = new BufferedReader( new FileReader( file ) )
    int            numbers[] = new int[10];

    String line;

    while( ( line = br.readLine() ) != null )
    {
        if( line.contains( "+" ) )
        {
            ... do stuff
        }
    }
}
catch( FileNotFoundException e )
{
    System.err.format("File does not exist \n");
}
finally
{
    if( reader !=null )
    {
        reader.close();
    }
}

References:

  1. How to read a large text file line by line using Java?
  2. https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
  3. https://docs.oracle.com/javase/7/docs/api/java/io/File.html
  4. Is there an eval() function in Java?
Community
  • 1
  • 1
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144