Then, I created a symlink in the /usr/bin directory called ReadFirstLine which points to the /usr/local/RFL/ReadFirstLine script -

#! /bin/bash
java -cp "/usr/local/RFL" ReadFirstLine "$1"

(/usr/local/RFL has ReadFirstLine.class)

Now, I can call ReadFirstLine from any directory like this -

$ ReadFirstLine simplefile.txt

This same script I want to call from a different machine. So from the remote machine I tried -

$ ssh username@xyz ReadFirstLine < localfile.txt

However, I got the error

Exception in thread "main" java.io.FileNotFoundException: (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at ReadFirstLine.main(ReadFirstLine.java:12)

How can I modify the Java Program / script such that this remote invocation works?

Your addendum is strange because that exact syntax worked for me.
–
WesApr 14 '14 at 19:45

1

oh, yes, that's correct. I need to go to bed I guess. I was still using the modified version of remote.sh which expected a file descriptor, not stdin. It will work if you are reading from stdin.
–
user61786Apr 14 '14 at 20:02

The problem is that the shell redirection (<) sends the file over the ssh tunnel. And the Java class is expecting not the file, but a string with the "filename" of a local file that will be read with a FileReader.

Instead of passing the filename to the FileReader, read from the standard input.

The local file, by definition, is on your local machine. The java program is on a remote machine. The remote machine does not know about your local file, and in any production situation probably does not have permission to read it. I can think of three general approaches to this situation:

1) Change your Java program to read from stdin instead of from a file. Then your current syntax $ ssh username@xyz ReadFirstLine < localfile.txt will work.

2) scp your file from the local machine to the remote machine before running the java program.
scp localfile.txt username@xyz
Make sure you put it in the directory which will be the working directory for your java program.

3) Mount a remote directory in your remote machine to you local machine. This has the most setup, but is the solution that get closest to what you are asking for. It will allow you to make changes to the file locally, and the remote machine will see the changes. See this answer for more about mounting a remote direcotry.