How to create a method from a text file?

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

Can anyone suggest a way to do this?

-one
source share
5 answers

consider this example , that it really downloads the source code, compiles and executes java code using a simpler program using the JavaCompiler API.

+1
source

Use JavaCompiler . It can compile code from String , so I'm sure that it can process code from a text file.


What do you think, instead of putting it in the main method, I can include it, for example, in a testing method and a calling method like this?

Put it anywhere. EG. see STBC and especially the source code . It provides a graphical interface and can compile code in the text area at the click of a button.

STBC GUI


this program needs tools.jar, but jre 7 does not have this !!

Have you tried reading the documentation provided for STBC? In particular:

System requirements

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

(*) The API that STBC uses is just an open interface for the compiler in tools.jar , which is distributed only with the JDK (although the "public JRE" JDK tools.jar acquire tools.jar ). This leads to some unusual requirements when launching either your own banner or web launch application.


Or in short, no JRE will have a JavaCompiler , only they have a JDK.

+1
source
  • Change the .txt file to a .java file,
  • add it to java project
  • Compile code
  • Execute methods
0
source

Download the file through standard java IO and then Groovy rate it for you:

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

0
source

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

0
source

Source: https://habr.com/ru/post/1393773/


All Articles