Adrian M
Adrian M

Reputation: 761

Where to put files for a command-line Java program?

I have written a Java program that takes in arguments and then executes. I pass in these arguments from the command line (I am on a Macbook Pro using Terminal, using the bash shell). Let's say the name of my program is prgm. I want to be able to say "prgm " from any directory in the shell and then have that program execute. So I figure I need to write a bash script to reference the Java files and take in arguments, and put that bash script somewhere in my PATH. Where do I put the bash file, and where do I put my Java files? Also, am I right to assume that I only need the .class (binary) Java files?

Upvotes: 2

Views: 384

Answers (1)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

Step-by-step:

  1. Assuming that the name of the Java executable if myjavaprog.
  2. Assuming that the name of your bash script is myscript.
  3. Make sure myscript is calling myjavaprog using absolute path and the desired arguments.
  4. call echo $PATH and you will see a bunch of paths: /some/path1:/some/other/path2:...
  5. Put your bash script in whatever path you want from the ones returned by echo $PATH.
  6. Go to a random path.
  7. Call you bash script bash myscript. See the execution of myjavaprog.

Tips:

  • If java program is for personal use only, put it in a path starting with /usr/ or even in your $HOME directory (and add that location to your PATH)
  • If java program must be shared with other users, put it in an accessible place, so that other users don't need to modify their PATH variable.

Upvotes: 1

Related Questions