开发者

How to build your non-gui Java program into a console program

开发者 https://www.devze.com 2022-12-28 05:31 出处:网络
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

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.


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.


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.


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

How to build your non-gui Java program into a console program

How to build your non-gui Java program into a console program

How to build your non-gui Java program into a console program

0

精彩评论

暂无评论...
验证码 换一张
取 消