简体   繁体   中英

Intercept Shell Command

I want to know a way in which I would be able to intercept shell commands

while I run a command to compile a java program like

javac xxx.java

I want the shell to execute a set of programs that would instrument the xxx.java file prior to the compilation by running a set of other commands as like.

./instrument xxx.java

and only after that the JVM must be called to compile. Is it possible to intercept commands in a shell,if not how could I achieve it without the user of the system being aware of his code being instrumented???

Sounds like you want a build tool. Maven is an excellent choice, and there are a variety of other great choices out there as well ( ant , gradle , make, etc.). Build tools allow you to tie together a complex series of actions to generate your final artifact without having to specify those steps each time.

Depends on the interpretation of your question. Sounds like your trying to be sneaky, and not use a maven build, so here it goes.

What you want is to intercept and run a version of the program under the same name. Basically you need to create an executable called

javac

that takes in the EXACT same parameters as the real javac. Then in your program, you can run whatever instrumentation you need on the files as parameters. Finally, you can call the real javac by doing a lookup in JAVA_HOME or whatever environment variable holds the path to the original java bin directory.

In order to get your version of javac to take priority, you need to add it to the HEAD of the PATH environment variable. This will ensure it is first to be checked for the program name. In linux, do that by this

PATH=/usr/myPrograms/java/bin:$PATH

Note: This solution will not work if the user explicitly types in the absolute fullpath to javac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM