Reputation: 4554
I'm looking to use jshell to replace bash for command line processing.
I've created a simple class fs in the file fs.jsh (yes poor naming) that has a number of utility functions like:
// file fs.jsh
class fs
{
static void println(String line)
{
System.out.println(line);
}
}
I know want to include fs.jsh from another file: e.g.
// helloworld.jsh
import fs.jsh
fs.println("Hello World");
The above code gives the error:
package fs does not exist
| import fs.jsh;
I've also tried:
import fs;
Which gives:
Error:
| '.' expected
| import fs;
So how do I import one script file from another.
Upvotes: 2
Views: 649
Reputation: 31888
One thing that you can make sure is to create an instance of the class before you access its method :
new fs().println("Hello World");
Another, do make sure the sequence in which the scripts are executed is fixed if one relies on the code of the other.
Scripts are run in the order in which they’re entered on the command line. Command-line scripts are run after startup scripts. To run a script after JShell is started, use the
/open
command.
Additionally, an import
without a package doesn't make much sense and you cannot have package
s in Jshell snippets.
The way you can do it is somewhat like:
some.jsh
sometwo.jsh
which calls it sometwo.jsh
Upvotes: 2