-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?

3
  • I google'd and found this: javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html Commented Nov 10, 2011 at 14:14
  • @Nerdtron Is your Google searching 1999? com.sun.tools.javac.Main.compile(.. Shudder Use the JavaCompiler! Commented Nov 10, 2011 at 14:21
  • LOL - it was the first result :) maybe it was the way I worded it. Commented Nov 10, 2011 at 16:21

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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);
@AndrewThompson this example gives a nullpointerException!! i cant run it!!
@AndrewThompson Please help me this example has an exception but if this work that's perfect for me!! Thanx
@KhashayarNapster can you show us the code where this throws this NPE. i mean line number?
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!!
|
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.

2 Comments

this program need tools.jar but jre 7 doesnt have this!!
@KhashayarNapster Set your JAVA_HOME and PATH variables, so that they point to a JDK, not a JRE.
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

1 Comment

I Want It too be dinamic not to go every each time too copy paste in the editor!!
0

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

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

Comments

0

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

1 Comment

Thanx Man But I Dont Get This!! :(

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.