1

I dont know how to describe it well, but i will try. Ok, i want to be able to build my java program so that when it opens, it will look and work exactly as it does in the console. So it reads the Scanner class and prints normally, and does everything it would do if it was in the console. Ive looked around for this and havent found anything. I can make a gui java program fairly easily, but i would rather have a terminal, console like program, that works exactly as the java console, thanks.

6
  • 4
    Have you tried "Start > Run > cmd"? Commented Apr 21, 2010 at 22:55
  • What do you mean? you want a terminal based program that reads input from terminal keyboard input and prints output on the terminal(stdout)?? Commented Apr 21, 2010 at 22:56
  • Are you looking for some kind of... terminal emulator? Commented Apr 21, 2010 at 22:57
  • Something like a terminal emulator, I know in c++ when you make a program it is automatically made into a terminal type program, but i havent found a way to make it like this in java. Commented Apr 21, 2010 at 22:59
  • Tim, can you explain further? when i compile my classes i get a jar file, which does nothing when i click on it, which i think is normal because its not gui Commented Apr 21, 2010 at 23:02

3 Answers 3

3

Based on your comment:

but i want to do hundreds of links at a time which works perfectly using the scanner nextLine() method, i can just post 100 or 200 links in the java console and it will automatically sort them out.

I'm guessing what you want is batch processing. You can have your 100 or 200 links in a text file, one per line. And then your Java program:

import java.io.*;

public class Batch{
    public static void main(String args[]){
    try{
        FileInputStream fstream = new FileInputStream("sample.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String line;

        while((line = br.readLine()) != null){
            //Do something with your line
            System.out.println(line);
        }
        in.close(); 
    }catch(IOException ioe){
        System.err.println(ioe.getMessage());
    }
}

}

You compile this program, open up a console and run it:

java Batch

It reads your sample.txt file and for each line it does something, in this case print it to the console.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, i thought about doing this, if i decide in the future to make it into a gui, i will use something like this. like when i paste the links in the textarea, the text area will save all the text to a temporary txt file and the load up the link like that. I just wanted to see if it was possible to make a java console like scilab or something. thanks tho.
1

You might be looking for the standard input and output members of the java.lang.System class:

class HelloWorld {

  public static void main(String... argv) {
    System.out.println("Hello, World!");
  }

}

For processing input, you can use Scanner on standard input:

Scanner scanner = new Scanner(System.in);

If you want to get really fancy, you can print some of your output to System.err, which is a PrintStream just like System.out.


From the comment, "when i compile my classes i get a jar file, which does nothing when i click on it, which i think is normal because its not gui," I think your problem is an operating system problem (Windows?), not a Java problem.

Windows maps the "Open" action for JAR files to run with javaw.exe, which doesn't create a console. You'll either need to modify the default file association on each machine, or create something like a batch file that overrides this default behavior.

You could write two programs: the first is your actual "console" Java application, and another is just a shell that uses Runtime.exec() to create a Windows console (cmd) and executes the first program within it.

There are also opensource projects (check Sourceforge) that wrap your JAR in a Windows executable.

7 Comments

lol, I know how to do that, I use that in my program, but i need to make it so that the compiled jar file of that, works exactly like the console would, make sense?
You mean you want to write a console?
If their is no built in function in Java which can do this, then i suppose I do, mean to write a console, but am hoping ive overlooked something java has which will save me the time of doing it.
To explain what im going for more: Im writing a program which gets an http link and sends it through post data to a site, but i want to do hundreds of links at a time which works perfectly using the scanner nextLine() method, i can just post 100 or 200 links in the java console and it will automatically sort them out. I want to avoid having use a gui textbox to input the links which will then cause all the links to be one long string, making me have to write more code to separate each link one by one, and sending them that way, hope that helps.
Erickson, thanks, I went on sourceforge and got Launch4j Executable Wrapper, i wrapped my jar into a console exe and it worked.
|
0

Use launch4j. It will create a exe of your non GUI application. In launch4j go to header section and check the console option. Done! Check console option

Select your jar file

Select jre

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.