Reputation: 75
I am writing a bash script that will let me run a java file from a different directory but I am not sure why my already compiled java file wont run. Relevant code:
#!/bin/bash
if [ "$1" == "JavaAdd" -o "$1" == "JavaAddBad" ]
then
echo "Testing $1"
`java ../Source/Java/"$1"`
else
echo "Invalid File"
fi
this script is under a Script directory. So both subdirectories Script and Source are in the same directory. My compiled java file is under /Source/Java
Upvotes: 0
Views: 158
Reputation: 262544
The parameter to java
is not a path or file name.
It is a class name.
You can tell Java where to find it by specifiying a classpath.
java -cp ../somewhere/classes:../../somewhereElse/x.jar com.me.MyClass
The class will then be looked for in all the locations on the classpath. Each location can be either a directory or a jar file.
Also note that the directory you give to the classpath needs to put to the root of your class package hierarchy. So if your class is called com.me.MyClass
and it is in a file at ../somewhere/classes/com/me/MyClass.class
, you need to include ../somewhere/classes
(not any of its subdirectories).
Also note that including a class path does not change the working directory for the program. It will still resolve relative paths when opening files based on the directory where you started it (completely unrelated to where the class files are).
Upvotes: 3