-1

I have a text (.txt) file that contains Java code! I want to create a method that includes this Java code and then call that method through the program.

Can anybody suggest a way to do this?

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Khashayar
  • 1,893
  • 3
  • 20
  • 28
  • I google'd and found this: http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html – Nerdtron Nov 10 '11 at 14:14
  • @Nerdtron Is your Google searching 1999? `com.sun.tools.javac.Main.compile(..` *Shudder* Use the [`JavaCompiler`](http://download.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html)! – Andrew Thompson Nov 10 '11 at 14:21
  • LOL - it was the first result :) maybe it was the way I worded it. – Nerdtron Nov 10 '11 at 16:21

5 Answers5

1

Use the JavaCompiler. It can compile code from a String, so I'm sure it could handle code from a text file.


Do you think instead of putting it in the main method I can put it in for example test method and call method like this?

Put it wherever you like. E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.

STBC GUI


this program need tools.jar but jre 7 doesnt have this!!

Did you try reading the documentation that is provided for the STBC? Notably:

System Requirements

STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).

(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.


Or shorter, no JRE will have a JavaCompiler, only JDKs have them.

Community
  • 1
  • 1
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
1

let consider this example what it does actually load the source code, compile and execute the java code by simpler program by using JavaCompiler API.

Muhammad Saifuddin
  • 1,197
  • 10
  • 26
  • Really good! Do you think instead of Putting it in the main method id can put it in for example test method and call method like this? Method m = clazz.getMethod("test", new Class[] { String[].class }); Object[] _args = new Object[] { new String[0] }; m.invoke(null, _args); – Khashayar Nov 10 '11 at 14:29
  • @AndrewThompson this example gives a nullpointerException!! i cant run it!! – Khashayar Nov 10 '11 at 14:59
  • @AndrewThompson Please help me this example has an exception but if this work that's perfect for me!! Thanx – Khashayar Nov 10 '11 at 15:21
  • @KhashayarNapster can you show us the code where this throws this NPE. i mean line number? – Muhammad Saifuddin Nov 10 '11 at 15:29
  • In This Line!! There is NPE!! But I cant Understand why!!!? compiler.getTask(null, null, null, null, null, fileObjects).call(); In Line 21 of the code!! – Khashayar Nov 10 '11 at 15:42
  • its like ToolProvider.getSystemJavaCompiler(); doesn't return the compiler object. check this way. if (compiler == null) { throw new NullPointerException("Cannot provide system compiler."); } – Muhammad Saifuddin Nov 10 '11 at 16:00
  • did you set JAVA_HOME variable. here is the [instructions how set Java Home and Path](http://www.coderanch.com/how-to/java/how-to-create-java-program#SettingJavahomeAndPath) and another thread is discussed this already [here](http://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api) – Muhammad Saifuddin Nov 10 '11 at 18:14
0
  1. Change the .txt file to a .java file,
  2. add it to your java project
  3. Compile the code
  4. Execute the methods
Stealth Rabbi
  • 9,370
  • 17
  • 89
  • 161
0

Load the file in through standard java IO and then have Groovy evaluate it for you:

http://groovy.codehaus.org/Embedding+Groovy

Roy Truelove
  • 20,716
  • 17
  • 105
  • 152
0

it's something like quine: http://www.nyx.org/%7Egthompso/quine.htm

c'quet
  • 335
  • 3
  • 7