LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2010, 08:04 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
JAVA Interpreter problem for Linux Commands


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 = "";
}

Command command = (Command)(myCommands.get(name));
if (command != null)
{
String result = command.execute(args);
System.out.println(result);
}
else
{
// error handling
System.err.println("Unrecognized command: " + line);
}
}


//////////////////////////////////////////////////////////////
// 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();
}
}
}

-------------------------------------------------
Output:


[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]#

Last edited by cmontr; 09-10-2010 at 08:06 PM.
 
Old 09-11-2010, 12:21 AM   #2
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

Here's an example error:
Quote:
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.

'Hope that helps!
 
Old 09-11-2010, 08:46 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by 14moose View Post
Hi -

Here's an example error:

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.

'Hope that helps!
Thanks, I got this running.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run Linux commands using a Java program? aparna.v Linux - Newbie 2 04-09-2010 09:37 AM
Running Linux Commands in Java jcafaro10 Programming 3 04-15-2009 12:10 PM
How to write an interactive fiction interpreter in Java? Jorophose Programming 6 04-13-2007 04:25 PM
java compilator and interpreter ??? gooma Linux - Software 15 12-29-2003 11:44 AM
java interpreter matt_w_lambert Programming 2 07-07-2003 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration