如何将非 GUI Java 程序构建为控制台程序
我不知道如何很好地描述它,但我会尝试。好的,我希望能够构建我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的评论:
我猜你想要的是批处理。您可以在一个文本文件中包含 100 或 200 个链接,每行一个。然后是您的 Java 程序:
}
您编译该程序,打开控制台并运行它:
它读取您的sample.txt 文件,并为每一行执行一些操作,在本例中将其打印到控制台。
Based on your comment:
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:
}
You compile this program, open up a console and run it:
It reads your sample.txt file and for each line it does something, in this case print it to the console.
您可能正在寻找
java.lang.System
类:为了处理输入,您可以在标准输入上使用 Scanner:
如果您想变得更奇特,您可以将一些输出打印到
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:For processing input, you can use Scanner on standard input:
If you want to get really fancy, you can print some of your output to
System.err
, which is aPrintStream
just likeSystem.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.
使用launch4j。它将创建非 GUI 应用程序的 exe。在 launch4j 中,转到标题部分并检查控制台选项。完毕!
Use launch4j. It will create a exe of your non GUI application. In launch4j go to header section and check the console option. Done!