ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi All,
I am working on a program like JShell command line interpreter for Linux commands. I am having errors and I appreciate if you can let me if you see the issue. I use Fedora 12 with JDK java version "1.6.0_19" installed.
Program A2.java after compilation is posted as well as the code. For example interpreter works fin except "ls" command. I also want to write new commands like grep, gzip, mv, cp, mkdir, cp etc...
Thanks in advance.
---------------------
Code:
import java.io.*;
import java.util.*;
public class A1shell
{
//////////////////////////////////////////////////////////////
// state
private int myNumCommands = 0;
private Map myCommands = new TreeMap();
private BufferedReader myConsole = new BufferedReader(
new InputStreamReader(System.in));
//////////////////////////////////////////////////////////////
// constructors
public A1shell ()
{
myCommands.put("date", new Date());
myCommands.put("echo", new Echo());
myCommands.put("cat", new Cat());
myCommands.put("ls", new Ls());
}
//////////////////////////////////////////////////////////////
// public functions
public String getCommand ()
{
myNumCommands++;
try
{
String line = "";
do
{
System.out.print(">/");
line = myConsole.readLine();
if (line == null)
{
return null;
}
else
{
line = line.trim();
}
}
while (line.equals("") || line.startsWith("#"));
return line;
}
catch (IOException e)
{
System.err.println("*** error reading from console: " + e);
return null;
}
}
public void runCommand (String line)
{
String name;
String args;
int firstSpace = line.indexOf(" ");
if (firstSpace > 0)
{
name = line.substring(0, firstSpace);
args = line.substring(firstSpace + 1);
}
else
{
name = line;
args = "";
}
//////////////////////////////////////////////////////////////
// Main
public static void main (String args[])
{
// create instance of this class to process input
A1shell shell = new A1shell();
String line = shell.getCommand();
while (line != null)
{
if (line.equals("logout"))
{
break;
}
else
{
shell.runCommand(line);
}
line = shell.getCommand();
}
}
}
[root@localhost bin]# javac -Xlint A1Shell.java
A1Shell.java:5: class A1shell is public, should be declared in a file named A1shell.java
public class A1shell
^
A1Shell.java:18: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
myCommands.put("date", new Date());
^
A1Shell.java:19: cannot find symbol
symbol : class Echo
location: class A1shell
myCommands.put("echo", new Echo());
^
A1Shell.java:20: cannot find symbol
symbol : class Cat
location: class A1shell
myCommands.put("cat", new Cat());
^
3 errors
1 warning
[root@localhost bin]#
A1Shell.java:20: cannot find symbol
symbol : class Cat
location: class A1shell
myCommands.put("cat", new Cat());
^
It means you've defined a class "A1Shell", in source file "A1Shell.java" ... and it expects to find another class,
... called "Cat" (presumably in another source file, "Cat.java").
Q: have you written class "Cat" yet?
SUGGESTION:
1. Write a simple Java program that uses a couple of public classes. Familiarize yourself with compiling and running.
2. Write another, equally simple Java program that parses command line (or text) input, like the word "cat", and acts on it.
It means you've defined a class "A1Shell", in source file "A1Shell.java" ... and it expects to find another class,
... called "Cat" (presumably in another source file, "Cat.java").
Q: have you written class "Cat" yet?
SUGGESTION:
1. Write a simple Java program that uses a couple of public classes. Familiarize yourself with compiling and running.
2. Write another, equally simple Java program that parses command line (or text) input, like the word "cat", and acts on it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.