如何将非 GUI Java 程序构建为控制台程序

发布于 2024-08-29 14:14:20 字数 197 浏览 8 评论 0原文

我不知道如何很好地描述它,但我会尝试。好的,我希望能够构建我的 java 程序,以便当它打开时,它的外观和工作方式与在控制台中完全相同。因此它会读取 Scanner 类并正常打印,并执行在控制台中会执行的所有操作。我环顾四周,没有发现任何东西。我可以相当容易地制作一个 gui java 程序,但我宁愿有一个终端、类似控制台的程序,它的工作原理与 java 控制台完全一样,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-09-05 14:14:20

根据您的评论:

但我想在以下位置创建数百个链接
一个可以完美使用的时间
扫描仪 nextLine() 方法,我可以
在 java 中发布 100 或 200 个链接
控制台,它会自动排序
他们出去了。

我猜你想要的是批处理。您可以在一个文本文件中包含 100 或 200 个链接,每行一个。然后是您的 Java 程序:

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

}

您编译该程序,打开控制台并运行它:

java Batch

它读取您的sample.txt 文件,并为每一行执行一些操作,在本例中将其打印到控制台。

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.

反差帅 2024-09-05 14:14:20

您可能正在寻找 java.lang.System 类:

class HelloWorld {

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

}

为了处理输入,您可以在标准输入上使用 Scanner:

Scanner scanner = new Scanner(System.in);

如果您想变得更奇特,您可以将一些输出打印到 System.err,它是一个PrintStream,就像System.out一样。


从评论中,“当我编译我的类时,我得到一个 jar 文件,当我点击它时它什么也不做,我认为这是正常的,因为它不是 gui,”我认为你的问题是操作系统问题(Windows?),不是Java问题。

Windows 将 JAR 文件的“打开”操作映射为使用 javaw.exe 运行,这不会创建控制台。您需要修改每台计算机上的默认文件关联,或者创建诸如批处理文件之类的内容来覆盖此默认行为。

您可以编写两个程序:第一个是实际的“控制台”Java 应用程序,另一个只是一个使用 Runtime.exec() 创建 Windows 控制台的 shell( cmd)并执行其中的第一个程序。

还有一些开源项目(请查看 Sourceforge)将 JAR 包装在 Windows 可执行文件中。

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.

一桥轻雨一伞开 2024-09-05 14:14:20

使用launch4j。它将创建非 GUI 应用程序的 exe。在 launch4j 中,转到标题部分并检查控制台选项。完毕!
检查控制台选项

选择你的 jar 文件

选择 jre

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文