views:

299

answers:

3

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.

+1  A: 

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.

erickson
Probably right... Or not? Who knows? XD
klez
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?
Robert
You mean you want to write a console?
erickson
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.
Robert
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.
Robert
Erickson, thanks, I went on sourceforge and got Launch4j Executable Wrapper, i wrapped my jar into a console exe and it worked.
Robert
If it worked you should vote up and accept his answer (the up arrow and check mark to the left of his answer).
Lawrence Johnston
I cant arrow up, but i checked it, thanks, im new, but ill get the hang if it, this site is amazing, i got many responses very quickly!
Robert
+2  A: 

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");
        DataInputStream  in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        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.

Cesar
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.
Robert
A: 

Are you asking if there is a way to make your jar file open a console and running in it when double clicked instead of you having to open the console yourself?

I am looking for a way to do that and I got here, and I see you couldn't explain yourself, so I thought it might be your problem too.

Zack