Hey guys, I have the idea of creating a custom Java terminal, I know what I need to do for commands, etc, but I have no idea on how to style it like someone would with a UNIX terminal/Windows Command Prompt, or prompt it so that whenever a new line is entered (besides within a command), it will come up as like

%USERNAME%@%OPERATING SYSTEM%:~$

If anyone is able to help me out a bit, post here or personal message me.

Recommended Answers

All 13 Replies

Maybe you are talking about the System.getProperty method,
here is my aproach:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CommandLine {
    public static void main(String[] args) {
    	
    	boolean active = true;
    	String username = System.getProperty("user.name");
    	String os = System.getProperty("os.name").trim();
    	try{
    		while(active){
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			System.out.print(username + "@" + os + ":~$ ");
    			String command = br.readLine();
    		}
    	}catch(Exception ex){
    		
    	}
    }
}

Maybe you are talking about the System.getProperty method,
here is my aproach:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CommandLine {
    public static void main(String[] args) {
    	
    	boolean active = true;
    	String username = System.getProperty("user.name");
    	String os = System.getProperty("os.name").trim();
    	try{
    		while(active){
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			System.out.print(username + "@" + os + ":~$ ");
    			String command = br.readLine();
    		}
    	}catch(Exception ex){
    		
    	}
    }
}

This is perfect! now I just need to work out how to design my own personal terminal.

This is perfect! now I just need to work out how to design my own personal terminal.

Happy coding friend :)

If anyone is able to help me with creating a GUI(?) for this program that resembles Terminal, while coded in Java?

Apparently this only works on Windows. Where-as I want this to be a multi-platform terminal, as I'm on ubuntu.

I already know how to create a text area and how to use them fluently, though. i want to create a terminal.

If you know text areas then this is just a simple display + input in a text area. Ricardo showed how to do the prompts. What exactly are you having difficulty with?

I want to create a terminal that is available on all platforms (Windows, Linux, Apple), that is coded completely in Java, and capable of handling preset commands (cf = create file, cof = compile file, etc). The only thing I'm having trouble with, is creating it to look like the Windows terminal, except with green text.

I'm baffled. You say "I already know how to create a text area and how to use them fluently". All you need is a text area with green lettering and a black background. That's trivial for someone who knows text areas "fluently". Perhaps you should show the code you have already and explain exactly where you are getting stuck.

import java.io.BufferedReader;
import java.io.InputStreamReader;
     
public class Main {
	  boolean _active = true;
     String _username = System.getProperty("user.name").toLowerCase();
     String _os = System.getProperty("os.name").trim().toLowerCase();
	
	public Main() {
  		 try {
   			while(_active) {
   				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   				System.out.print(_username + "@" + _os + ":~$ ");
   				String command = br.readLine();
   					if(command.equals("cf")) {
   						new commandCreateFile();
   					} else if(command.equals("cof")) {
   						new commandCompile();
   					} else if(command.equals("help")) {
   						System.out.println("Commands");
   						System.out.println(" cf              - Creates .java files, does not compile.");
   						System.out.println(" ccf             - Creates .java files, compiles on creation.");
   						System.out.println(" help            - Shows help documentation.");
   					} else if(command.equals("exit")) {
   						System.out.print("Are you sure you want to exit? (Y/N) ");
   						String exit = br.readLine();
   						if(exit.equalsIgnoreCase("y")) {
   						System.exit(0);
   						}
   					} else if(command.isEmpty()) {
   					
   					} else {
   						System.out.println("\"" + command + "\" does not exist. Please review the \"help\" menu");
   					}
   			}
   		} catch(Exception ex) {
    		System.out.println("There was a problem: " + ex);
    		}
   }
			   
	public static void main(String[] args) {
     new Main();
	}
}
import java.util.*;
import java.io.*;

public class commandCreateFile {
	boolean _active = true;
   String _username = System.getProperty("user.name").toLowerCase();
   String _os = System.getProperty("os.name").trim().toLowerCase();
   String fileName, create, option;
   
	public commandCreateFile() {
		try {
			System.out.print(_username + "@" + _os + ":~/create$ ");
			Scanner kbd = new Scanner(System.in);
				String userLine = kbd.nextLine();
				
			Scanner read = new Scanner(userLine);
				option = read.next();
				fileName = read.next();

			FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java"));
	
			if(userLine.equals(option + " " + fileName)) {
			    if(option.equals("-a")) {
					// Option = -a, creates standard file with main class.
					create.write("public class " + fileName + " {\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		System.out.println(\"Welcome to your new program!\");\n");
					create.write("	}\n");
					create.write("}");
			    } else if(option.equals("-c")) {
					// Option = -c , creates standard file with overloaded constructor & main class.
					create.write("public class " + fileName + " {\n");
					create.write("	public " + fileName + "() {\n");
					create.write("		System.out.println(\"Welcome to your new program!\");\n");
					create.write("	}\n");
					create.write("\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		new " + fileName + "();\n");
					create.write("	}\n");
					create.write("}");
			    } else if(option.equals("-j")) {
					// Option = -j, creates GUI within constructor w/ single JLabel.
					create.write("import javax.swing.*;\n");
					create.write("import java.awt.*;\n");
					create.write("import java.awt.event.*;\n");
					create.write("\n");
					create.write("public class " + fileName + " extends JFrame {\n");
					create.write("	private static final int HEIGHT = 50;\n");
					create.write("	private static final int WIDTH = 400;\n");
					create.write("\n");
					create.write("	private JLabel welcomeJ;\n");
					create.write("\n");
					create.write("	public " + fileName + "() {\n");
					create.write("    super(\"Welcome to your program - " + fileName + "\");\n");
					create.write("		Container pane = getContentPane();\n");
					create.write("    setLayout(new FlowLayout());\n");
					create.write("\n");
					create.write("		welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n");
					create.write("\n");
					create.write("		pane.add(welcomeJ);\n");
					create.write("\n");
					create.write("	   setSize(WIDTH, HEIGHT);\n");
					create.write("	   setVisible(true);\n");
					create.write("	   setResizable(false);\n");
					create.write("	   setDefaultCloseOperation(EXIT_ON_CLOSE);\n");
					create.write("	}\n");
					create.write("\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		new " + fileName + "();\n");
					create.write("	}\n");
					create.write("}");
			    }
			} else {
				System.out.println("Error in syntax. Please review the \"help\" menu");
			}
			create.close();
		   } catch(IOException e) {
			System.out.println("There was an error: " + e);
		   } catch(InputMismatchException ex) {
			System.out.println("There was an error: " + ex);
		   }
	}

	public static void main(String[] args) {
		new commandCreateFile();
	}
}

The text area does seem like a probable idea, except what if the user tries to resize the frame? How would I make it so the text area expanded with the frame?

import java.io.BufferedReader;
import java.io.InputStreamReader;
     
public class Main {
	  boolean _active = true;
     String _username = System.getProperty("user.name").toLowerCase();
     String _os = System.getProperty("os.name").trim().toLowerCase();
	
	public Main() {
  		 try {
   			while(_active) {
   				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   				System.out.print(_username + "@" + _os + ":~$ ");
   				String command = br.readLine();
   					if(command.equals("cf")) {
   						new commandCreateFile();
   					} else if(command.equals("cof")) {
   						new commandCompile();
   					} else if(command.equals("help")) {
   						System.out.println("Commands");
   						System.out.println(" cf              - Creates .java files, does not compile.");
   						System.out.println(" ccf             - Creates .java files, compiles on creation.");
   						System.out.println(" help            - Shows help documentation.");
   					} else if(command.equals("exit")) {
   						System.out.print("Are you sure you want to exit? (Y/N) ");
   						String exit = br.readLine();
   						if(exit.equalsIgnoreCase("y")) {
   						System.exit(0);
   						}
   					} else if(command.isEmpty()) {
   					
   					} else {
   						System.out.println("\"" + command + "\" does not exist. Please review the \"help\" menu");
   					}
   			}
   		} catch(Exception ex) {
    		System.out.println("There was a problem: " + ex);
    		}
   }
			   
	public static void main(String[] args) {
     new Main();
	}
}
import java.util.*;
import java.io.*;

public class commandCreateFile {
	boolean _active = true;
   String _username = System.getProperty("user.name").toLowerCase();
   String _os = System.getProperty("os.name").trim().toLowerCase();
   String fileName, create, option;
   
	public commandCreateFile() {
		try {
			System.out.print(_username + "@" + _os + ":~/create$ ");
			Scanner kbd = new Scanner(System.in);
				String userLine = kbd.nextLine();
				
			Scanner read = new Scanner(userLine);
				option = read.next();
				fileName = read.next();

			FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java"));
	
			if(userLine.equals(option + " " + fileName)) {
			    if(option.equals("-a")) {
					// Option = -a, creates standard file with main class.
					create.write("public class " + fileName + " {\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		System.out.println(\"Welcome to your new program!\");\n");
					create.write("	}\n");
					create.write("}");
			    } else if(option.equals("-c")) {
					// Option = -c , creates standard file with overloaded constructor & main class.
					create.write("public class " + fileName + " {\n");
					create.write("	public " + fileName + "() {\n");
					create.write("		System.out.println(\"Welcome to your new program!\");\n");
					create.write("	}\n");
					create.write("\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		new " + fileName + "();\n");
					create.write("	}\n");
					create.write("}");
			    } else if(option.equals("-j")) {
					// Option = -j, creates GUI within constructor w/ single JLabel.
					create.write("import javax.swing.*;\n");
					create.write("import java.awt.*;\n");
					create.write("import java.awt.event.*;\n");
					create.write("\n");
					create.write("public class " + fileName + " extends JFrame {\n");
					create.write("	private static final int HEIGHT = 50;\n");
					create.write("	private static final int WIDTH = 400;\n");
					create.write("\n");
					create.write("	private JLabel welcomeJ;\n");
					create.write("\n");
					create.write("	public " + fileName + "() {\n");
					create.write("    super(\"Welcome to your program - " + fileName + "\");\n");
					create.write("		Container pane = getContentPane();\n");
					create.write("    setLayout(new FlowLayout());\n");
					create.write("\n");
					create.write("		welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n");
					create.write("\n");
					create.write("		pane.add(welcomeJ);\n");
					create.write("\n");
					create.write("	   setSize(WIDTH, HEIGHT);\n");
					create.write("	   setVisible(true);\n");
					create.write("	   setResizable(false);\n");
					create.write("	   setDefaultCloseOperation(EXIT_ON_CLOSE);\n");
					create.write("	}\n");
					create.write("\n");
					create.write("	public static void main(String[] args) {\n");
					create.write("		new " + fileName + "();\n");
					create.write("	}\n");
					create.write("}");
			    }
			} else {
				System.out.println("Error in syntax. Please review the \"help\" menu");
			}
			create.close();
		   } catch(IOException e) {
			System.out.println("There was an error: " + e);
		   } catch(InputMismatchException ex) {
			System.out.println("There was an error: " + ex);
		   }
	}

	public static void main(String[] args) {
		new commandCreateFile();
	}
}

The text area does seem like a probable idea, except what if the user tries to resize the frame? How would I make it so the text area expanded with the frame?

Maybe check here:http://www.codeguru.com/forum/showthread.php?t=494250

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.