📄 jshell.html
字号:
<html>
<head>
<title>
JShell: A Java Command Shell
</title>
</head>
<body>
<center>
<font size="+3"><b>JShell 0.2</b></font><br>
<i>Jack Orenstein</i><br>
<i>Geophile, Inc.</i><br>
<i><a href="mailto:jao@mediaone.net">jao@mediaone.net</a></i><br>
</center>
<hr><a href="#section1">1. What is JShell?</a><br><a href="#section2 ">
2
.
Examples
</a><br><a href="#section3">3. Configuring and Running JShell</a><br><a href="#section4">4. Files</a><br><a href="#section5">5. Wildcards</a><br><a href="#section6">6. Variables</a><br><a href="#section7">7. Strings</a><br><a href="#section8">8. Piping</a><br><a href="#section9">9. Redirection</a><br><a href="#section10">10. Background tasks</a><br><a href="#section11">11. Command recall</a><br><a href="#section12">12. Command usage</a><br><a href="#section13">13. Extending JShell</a><br><a href="#section14">14. Known problems</a><br><a href="#section15">15. Helping out with JShell</a><br>
<hr><h1><a name="section1">1. What is JShell?</a></h1>
<p>
JShell is a UNIX-like shell written in Java, and intended primarily
for working with Java programs. It was written to overcome the
problems in working with Java on the Psion 5mx:
</p>
<ul>
<li>
eshell, the EPOC shell which can be used on the 5mx, has no
environment variables.
</li>
<li>
The command to start a Java program is quite lengthy, and this
problem is exacerbated by the lack of environment variables. (Not
to mention the 5mx's keyboard, which is good for a PDA but cramped
nonetheless.)
</li>
<li>
Every time a Java program is started, the Java Virtual Machine has
to be loaded. On the 5mx this is time-consuming.
</li>
<li>
If you want to compile on the 5mx, javac is your only
choice. Because javac is written in Java, loading of the JVM is
required. Also, the command for running javac is lengthy.
</li>
<li>
Each Java program that executes simultaneously requires another
JVM. Not only is there an additional delay as each JVM starts, the
memory requirements of each JVM are considerable, especially on a
machine with 16 meg RAM. This is a problem if you want to pipe the
output from one Java program to another. (I'm not even sure whether
eshell supports pipes.)
</li>
</ul>
<p>
Because JShell is written in Java, running JShell starts a
JVM. This is the only JVM you need. You can use JShell to run
other Java programs (i.e. classes with a public static void
main(String[]) method), and these run inside JShell's JVM as a
thread. The JShell commands are Java classes which also run in
the same JVM. You can extend the set of JShell commands. In fact,
you are encouraged to do this and distribute your work to other
JShell users.
</p>
<p>
JShell is designed to support development in Java. It is expected
that you will be going through edit/compile/run/debug
cycles. Java classes other than the JShell commands are reloaded,
using a new class loader, each time the command is run. This
permits the JShell JVM to work with different versions of the
"same" class as you edit and recompile your Java source. You
don't need to exit and restart JShell to pick up the latest
versions of your Java classes.
</p>
<p>
While JShell was inspired by the Psion 5mx, it can be used on any
Java platform. I've developed and used JShell on Windows NT,
Linux, and EPOC.
</p>
<hr><h1><a name="section2 ">
2
.
Examples
</a></h1>
<p>
The design of JShell was intended to be familiar to users of
shells such as sh and csh. JShell is much simpler although
somewhat less powerful. Here are some examples to give you an
overview of JShell.
</p>
<p>
List the files in the current directory, with details on each file:
</p>
<pre>
5> ls :l
</pre>
<p>
The 5 is a "job number". Each command that you run gets its own
job number. The default prompt character is '>'. The ':'
character is used for flags, by default. The traditional flag
character, '-', is awkward to type on the 5mx while ':' is more
accessible, (at least this is true on my 5mx; I've seen other
keyboard layouts on other 5mxs). Both the prompt and flag
characters can be changed.
</p>
<p>
List the java sources in the parent directory:
</p>
<pre>
6> ls ../*.java
</pre>
<p>
List the files in the current directory, recursively, with
details:
</p>
<pre>
7> ls :lR
</pre>
<p>
You can examine the current character used for flags:
</p>
<pre>
8> env jshell.flag
:
</pre>
<p>
And change it to something else:
</p>
<pre>
9> set jshell.flag -
jshell.flag=-
</pre>
<p>
Then you can do this, for example:
</p>
<pre>
10> ls -lR
</pre>
<p>
Here is how to compile all the java sources in this directory:
</p>
<pre>
11> javac *.java
</pre>
<p>
Type all the .txt files in the current directory:
</p>
<pre>
12> cat *.txt
</pre>
<p>
Make a new directory, foobar, and copy all the .txt files to it.
</p>
<pre>
13> mkdir foobar; cp *.txt foobar
</pre>
<p>
Compile a bunch of Java sources in the background.
</p>
<pre>
14> javac *.java > errors.txt &
</pre>
<p>
An & at the end of the line causes the command-line you entered
(including all statements separated by semi-colons) to be
executed in the background. You will get a new command prompt
immediately after running the above command, but the file
files.txt won't be complete until the command finishes executing.
</p>
<p>
You can view executing jobs using the jobs command, e.g.
</p>
<pre>
15> jobs
123: javac *.java > errors.txt &
</pre>
<p>
The number before the colon is the "job" number. These numbers
are unique within one execution of JShell. You can kill executing
jobs by using the kill command. For example, the "javac *.java >
errors.txt &" command can be kill like this:
</p>
<pre>
16> kill 123
</pre>
<hr><h1><a name="section3">3. Configuring and Running JShell</a></h1>
<h2>EPOC</h2>
<p>
JShell.sis should be installed to your D drive. The batch file
jshell.bat is written to assume that JShell is installed in
D:\Java\JShell. If you don't have a D drive, it is doubtful that
you will have enough memory for Java's classes.zip file, the
JShell jar files and have enough memory left over to run JShell
effectively. However, if you want to try this, you will need to
modify jshell.bat and change all the references to the D drive to
point to the C drive instead.
</p>
<p>
To run JShell, you will need to have installed eshell first. You
can download eshell from the Symbian web site
<a href="http://www.symbiandevnet.com/downloads/progs/e-shell.html">
here</a>.
</p>
<p>
Once you have eshell and JShell installed, go to the directory
D:\Java\JShell. Run the command "jshell". In a few seconds, you
should see a console with this prompt:
</p>
<pre>
0>
</pre>
<p>
The first command will take a few seconds to run as the JShell
class files get loaded. Subsequent commands should be faster.
</p>
<h2>Windows and UNIX</h2>
<p>
Add jshell.jar and javacup.jar to your CLASSPATH. Then run jshell as
follows:
</p>
<pre>
java jshell.JShell
</pre>
<hr><h1><a name="section4">4. Files</a></h1>
JShell uses UNIX-style file names with forward slashes and no
concept of a logical drive (e.g. C: or D:). On DOS-like operating
systems, (DOS, any version of Windows, EPOC), the drive letter is
just the first part of the file name. So the DOS or EPOC path
C:\foo\bar.txt becomes /C/foo/bar.txt. File names are
case-sensitive only if the underlying operating system is, (so
JShell on DOS and EPOC is case-insensitive, while JShell on UNIX
is case-sensitive).
<hr><h1><a name="section5">5. Wildcards</a></h1>
<p>
DOS has two wildcard characters, * which matches any number of
characters, and ? which matches any one character. UNIX has two
different kinds of wildcarding, "glob" patterns such as
foo*.{cc,hh}, and regular expressions.
</p>
<p>
JShell takes an intermediate position. It uses UNIX-style glob
patterns whenever a pattern is required. Regular expressions can
be more powerful, but glob patterns should be sufficient for most
purposes.
</p>
<p>
To summarize glob patterns:
</p>
<table cellpadding="5">
<tr>
<td>*</td>
<td>matches zero or more characters.</td>
</tr>
<tr>
<td>?</td>
<td>matches exactly one character.</td>
</tr>
<tr>
<td>[abc]</td>
<td>matches a, b or c.</td>
</tr>
<tr>
<td>[a-zA-Z0-9]</td>
<td>matches any letter or digit.</td>
</tr>
<tr>
<td>{foo,bar}</td>
<td>matches foo or bar.</td>
</tr>
<tr>
<td>xyz</td>
<td>matches xyz</td>
</tr>
</table>
<p>
For example, this command lists all the .java and .class files
in the current directory:
</p>
<pre>
> ls *.{java,class}
</pre>
<hr><h1><a name="section6">6. Variables</a></h1>
<p>
A JShell variable begins with a letter or underscore and may consist
of letters, underscores, digits and dots. Dots are permitted to
provide for namespaces. All variables set by JShell begin with
jshell. For example, jshell.prompt is the JShell prompt string.
</p>
<p>
Variables are set using the set command, e.g.
</p>
<pre>
100> set FOO xyz
</pre>
<p>
and examined using the env command, e.g.
</p>
<pre>
101> env FOO
FOO=xyz
</pre>
<p>
or the echo command:
</p>
<pre>
102> echo $FOO
xyz
</pre>
<p>
Note that variable names must be prefixed with $ to obtain the
variable's value.
</p>
<p>
JShell's environment variables include the JVMs System properties.
So if you type the "env" command, you'll see values for
java.class.path, os.name, and all the other System properties. You'll
also see JShell properties such as the current directory (jshell.dir),
and whether output pauses after every page of output (jshell.page).
</p>
<p>
Here is a complete list of the environment variables defined by
JShell. (Their values can be obtained by running the command
"env 'jshell*'"):
</p>
<table cellpadding="5">
<tr>
<td>jshell.buffer</td>
<td>Controls whether a buffer is used in handling console output.</td>
</tr>
<tr>
<td>jshell.columns</td>
<td>The number of columns of the output console.</td>
</tr>
<tr>
<td>jshell.dir</td>
<td>The current directory. The value of this variable can be observed and modified using the commands pwd, dirs, cd, pushd, and popd.</td>
</tr>
<tr>
<td>jshell.flag</td>
<td>The character used to denote a flag.</td>
</tr>
<tr>
<td>jshell.history_size</td>
<td>The number of previous commands tracked by JShell.</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -