当前位置: 代码迷 >> java >> 如何在Java中写入控制台?
  详细解决方案

如何在Java中写入控制台?

热度:81   发布时间:2023-07-17 20:20:16.0

我正在eclipsecommand line Java程序上工作。 我使用System.out.println写入控制台。 当我使用eclipse运行它时,它起作用了,但是当我将其编译为jar文件并通过cmd运行时,它没有在屏幕上写入任何内容。 我查找的所有内容都表示使用System.out.println写入命令行。 我该怎么办? 这是我的代码:

package cpac;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;




public class packfile {
    static double vernum = 1.1;
    public static void saveUrl(final String in2, final String urlString)
            throws MalformedURLException, IOException {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream(in2);

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
    }
    public static void main (String[] args) throws IOException {
        int where = 0;
        System.out.println("CPac Version " + vernum);
        for (String s: args) {
            if (s.equals("update")) {
            java.io.File file = new java.io.File("cpac.jar");
            file.delete();
            saveUrl("cpac.jar", "http://example.com/package/cpac.jar");
            return;
            }



            if (s.equals("install")) {
                System.out.println("install");
                URL oracle = new URL("http://example.com/package/" + args[where + 1] +"/package.pac");
                BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

                String inputLine;
                String data = null;
                while ((inputLine = in.readLine()) != null){
                data = inputLine;
            }
                in.close();
                saveUrl(data, "http://example.com/package/" + args[where + 1] +"/" + data);
                System.out.println("Done!");

            }
            where = where + 1;
            }
        }
}

编辑:我刚刚读到一些内容,说您不能通过在cmd中键入它们的名称来运行jar文件。 有什么方法可以不必键入一个长命令而无需额外的文件吗?

这将有助于查看您在命令行上输入的内容。 希望它看起来像这样。

java -cp <filename.jar> cpac.packfile

“在Eclipse中工作”-一个IDE使您无法了解事情的真正运行方式。

您不运行JAR文件; 您运行JRE并告诉它使用JAR文件查找在META-INF / manifest.mf中指定的主类。

控制台中是否没有消息? 您没有反馈吗? 如果 ,则主类将运行。 如果您的主类运行,则在您打印到控制台时它将写入命令外壳。

  相关解决方案