I have a java CLI script that converts rgb names to hexcodes (e.g. 144 132 146 becomes #908492). However, I want to be able to run it from any terminal. I put a bash script in the same folder so it could run the file:
The bash script is simple enough, just:
#!/bin/bash
java rgb2hexConv $1 $2 $3
However, when I run the code through the PATH, I get errors related to the file rgb2hexConv not being found.
Diagram:
/
/home/
/home/me/
/home/me/someRandomDir/ (running from here does not work)
/home/me/utils/ (in path) (running from here works)
- rgb2hex (bash script)
- rgb2hexConv.class (java program)
My guess is that it's looking for rgb2hexConv in /home/me/someRandomDir/ as opposed to /home/me/utils/. Is there anyway to get the directory of the bash script?
EDIT: Changing the script to use ./rgb2hexConv gives the following:
Exception in thread "main" java.lang.NoClassDefFoundError: //rgb2hexConv
Caused by: java.lang.ClassNotFoundException: ..rgb2hexConv
// long stack trace removed
Could not find the main class: ./rgb2hexConv. Program will exit.
(The bit at the end of the first line is not a comment, but actual output)
EDIT 2: After an attempt at using $0 the following output was recieved
Exception in thread "main" java.lang.NoClassDefFoundError: /home/me/utils/rgb2hex/rgb2hexConv Caused by: java.lang.ClassNotFoundException: .home.me.utils.rgb2hex.rgb2hexConv // Long Stack Trace Could not find the main class: /home/me/utils/rgb2hex/rgb2hexConv. Program will exit.
Two things about this:
- $0 contains the file name as well as a directory
- The
java
command seems to be replacing "/" with ".".