Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use java to write a linux tool

I would like to use use java to write a tool to use under linux so that I can call it like I call "ls". What do I need to do? I planed to:

  1. write a java with main function take my arguments, do the job. How do I return results? Java main cannot return string.

  2. Where is the best place to add my jar to in the system? /user/bin?

  3. how can I call it with minimum typing? I don't want to type java jar XXXXXX com.mynamespace.myfunction ...... How can i just call it by using a simple name like "ls"?

Thanks a lot

like image 784
Frank Avatar asked Jun 10 '26 15:06

Frank


1 Answers

You can create a bash file /usr/bin/custom_program pointing to your jar:

#!/usr/bin/bash
java -jar /path/to/your/java.jar # add any custom command line switches here

Don't forget to mark it as executable with sudo chmod u+x /usr/bin/custom_program.

Your output would be given as System.out.println() calls in your program -- no need to return String from main.

Then, you can call your java program with custom_program with the help of the bash script above.

like image 109
pascalhein Avatar answered Jun 12 '26 04:06

pascalhein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!