jar 无法在命令行上运行

发布于 2024-12-22 07:25:52 字数 5172 浏览 5 评论 0原文

我确实通过谷歌和各种论坛进行了搜索(也是这个)。

我创建了一个应用程序,一个 java 基准测试,并想要创建一个可运行的 jar 文件,以便在其他计算机上使用该程序。不幸的是,jar 不起作用,一切都通过制作 jar 文件的代码完美完成,程序在命令行上运行。我尝试了在这个论坛上找到的技巧来修复我的 jar 创建,但效果不佳。

奇怪的是,当我编译 JavaBenchmark.java 文件时,我不仅得到一个文件(JavaBenchmark.class),而且还得到 JavaBenchmark$1.class :O (有人知道为什么吗?)

所以我请你检查我的代码是否有可能有些问题,我必须说它是一个 GUI 应用程序。

import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaBenchmark  implements ActionListener
{
    private Frame mainWindow;
    private Button exit;
    private String dateAndTime;
    private TextArea values;
    private String stringMaxMemory;
    private String stringFreeMemory;
    private String stringTotalFreeMemory;
    private String stringAllocatedMemory;

    public JavaBenchmark(String s)
    {
        Date myDate = new Date();
        dateAndTime = String.format("%tA, %<tF", myDate);

        File[] roots = File.listRoots();

        mainWindow = new Frame(s);
        mainWindow.setSize(640,480);
        mainWindow.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}});

        String version = System.getProperty("java.version");
        String jvmversion = System.getProperty("java.jvm.version");
        String checkedJvmVersion;
        if (jvmversion == null)
        {
            checkedJvmVersion = "Java Virtual Machine version: N/A";
        }
        else
        {
            checkedJvmVersion = "Java Virtual Machine version: " + jvmversion;
        }
        String jvmname = System.getProperty("java.vm.name");
        String osname = System.getProperty("os.name");
        String osarchitecture = System.getProperty("os.arch");
        String osversion = System.getProperty("os.version");
        String processor = System.getenv("PROCESSOR_IDENTIFIER");
        int processorCores = Runtime.getRuntime().availableProcessors();

        Runtime runtime = Runtime.getRuntime();
        double freeMemory = runtime.freeMemory();
        double allocatedMemory = runtime.totalMemory();
        double maxMemory = runtime.maxMemory();
        double totalFreeMemory = (freeMemory + (maxMemory - allocatedMemory));
        stringFreeMemory = String.format("%5.2f", (freeMemory)/1024/1024);
        stringAllocatedMemory = String.format("%5.2f", (allocatedMemory)/1024/1024);
        stringMaxMemory = String.format("%5.2f", (maxMemory)/1024/1024);
        stringTotalFreeMemory = String.format("%5.2f", (totalFreeMemory)/1024/1024);

        exit = new Button("Exit"); exit.addActionListener(this);
        values = new TextArea(30, 120);

    Panel exitButton = new Panel();

    exitButton.add(exit);

    mainWindow.add(values, "North");        
    mainWindow.add(exitButton);

    values.append("Your Java benchmark, as on: " + dateAndTime + "\n\n");
    values.append("Java version: " + version + "\n");
    values.append("Java Virtual machine version: " + checkedJvmVersion + "\n");
    values.append("Java Virtual Machine name: " + jvmname + "\n");
    values.append("\n");
    values.append("Operating System: " + osname + "\n" + osarchitecture + " os version: " + osversion + "\n");
    values.append("\n");
    values.append("Processor: " + processor + " (number of cores: " + processorCores + ")\n");
    values.append("\n");
    values.append("Memory info: \n");
    values.append("Maximum RAM memory for JVM:     " + stringMaxMemory + " Mb\n");
    values.append("Allocated RAM memory for JVM:     " + stringAllocatedMemory + " Mb\n");
    values.append("Free RAM memory for JVM:     " + stringFreeMemory + " Mb\n");
    values.append("Total free RAM memory for JVM:     " + stringTotalFreeMemory + " Mb\n\n\n");
    values.append("HardDrive, and VirtualDrive details:\n");
    for (File root : roots) {
          if (root.getTotalSpace() == 0)
          {
              continue;
          }
          else 
          {
              values.append("Disk: " + root.getAbsolutePath() + " space allocation:\n");
              values.append("Total space :"); 
              values.append(String.format("%5.2f", Double.valueOf(root.getTotalSpace())/1024/1024/1024));
              values.append(" Gb\n");
              values.append("Free space : ");
              values.append(String.format("%5.2f", Double.valueOf(root.getFreeSpace())/1024/1024/1024));
              values.append(" Gb\n");
              values.append("Occupied disk space : ");
              values.append(String.format("%5.2f", Double.valueOf(root.getTotalSpace() - root.getFreeSpace())/1024/1024/1024));
              values.append(" Gb\n\n");

          }
        }

    mainWindow.pack();              //Creating the window
    mainWindow.setLocationRelativeTo(null);         //true: position at (0,0)     false: position at center
    mainWindow.setResizable(false);         //Intuitively known commands
    mainWindow.setVisible(true);        //Intuitively known commands
}

public static void main(String[] args) 
{
    new JavaBenchmark("Display");
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==exit) 
    {
        System.exit(0);
    }
}
}

I did search through google and various forums, (this one as well).

I have created an application, a java benchmark, and wanted to create a runnable jar file, to use the program on other machines. Unfortunately, the jar is not working, everything is done perfect with the code to make jar file, the program runs on command line. I tried tricks found on this forum to fix my jar creation, but it didn't work as well.

Strangely enough, when i compile the JavaBenchmark.java file i do not get only one file (JavaBenchmark.class), but also JavaBenchmark$1.class :O (anyone knows why?)

So I ask you to check my code if THERE might be some problems, I must say its a GUI app.

import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaBenchmark  implements ActionListener
{
    private Frame mainWindow;
    private Button exit;
    private String dateAndTime;
    private TextArea values;
    private String stringMaxMemory;
    private String stringFreeMemory;
    private String stringTotalFreeMemory;
    private String stringAllocatedMemory;

    public JavaBenchmark(String s)
    {
        Date myDate = new Date();
        dateAndTime = String.format("%tA, %<tF", myDate);

        File[] roots = File.listRoots();

        mainWindow = new Frame(s);
        mainWindow.setSize(640,480);
        mainWindow.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}});

        String version = System.getProperty("java.version");
        String jvmversion = System.getProperty("java.jvm.version");
        String checkedJvmVersion;
        if (jvmversion == null)
        {
            checkedJvmVersion = "Java Virtual Machine version: N/A";
        }
        else
        {
            checkedJvmVersion = "Java Virtual Machine version: " + jvmversion;
        }
        String jvmname = System.getProperty("java.vm.name");
        String osname = System.getProperty("os.name");
        String osarchitecture = System.getProperty("os.arch");
        String osversion = System.getProperty("os.version");
        String processor = System.getenv("PROCESSOR_IDENTIFIER");
        int processorCores = Runtime.getRuntime().availableProcessors();

        Runtime runtime = Runtime.getRuntime();
        double freeMemory = runtime.freeMemory();
        double allocatedMemory = runtime.totalMemory();
        double maxMemory = runtime.maxMemory();
        double totalFreeMemory = (freeMemory + (maxMemory - allocatedMemory));
        stringFreeMemory = String.format("%5.2f", (freeMemory)/1024/1024);
        stringAllocatedMemory = String.format("%5.2f", (allocatedMemory)/1024/1024);
        stringMaxMemory = String.format("%5.2f", (maxMemory)/1024/1024);
        stringTotalFreeMemory = String.format("%5.2f", (totalFreeMemory)/1024/1024);

        exit = new Button("Exit"); exit.addActionListener(this);
        values = new TextArea(30, 120);

    Panel exitButton = new Panel();

    exitButton.add(exit);

    mainWindow.add(values, "North");        
    mainWindow.add(exitButton);

    values.append("Your Java benchmark, as on: " + dateAndTime + "\n\n");
    values.append("Java version: " + version + "\n");
    values.append("Java Virtual machine version: " + checkedJvmVersion + "\n");
    values.append("Java Virtual Machine name: " + jvmname + "\n");
    values.append("\n");
    values.append("Operating System: " + osname + "\n" + osarchitecture + " os version: " + osversion + "\n");
    values.append("\n");
    values.append("Processor: " + processor + " (number of cores: " + processorCores + ")\n");
    values.append("\n");
    values.append("Memory info: \n");
    values.append("Maximum RAM memory for JVM:     " + stringMaxMemory + " Mb\n");
    values.append("Allocated RAM memory for JVM:     " + stringAllocatedMemory + " Mb\n");
    values.append("Free RAM memory for JVM:     " + stringFreeMemory + " Mb\n");
    values.append("Total free RAM memory for JVM:     " + stringTotalFreeMemory + " Mb\n\n\n");
    values.append("HardDrive, and VirtualDrive details:\n");
    for (File root : roots) {
          if (root.getTotalSpace() == 0)
          {
              continue;
          }
          else 
          {
              values.append("Disk: " + root.getAbsolutePath() + " space allocation:\n");
              values.append("Total space :"); 
              values.append(String.format("%5.2f", Double.valueOf(root.getTotalSpace())/1024/1024/1024));
              values.append(" Gb\n");
              values.append("Free space : ");
              values.append(String.format("%5.2f", Double.valueOf(root.getFreeSpace())/1024/1024/1024));
              values.append(" Gb\n");
              values.append("Occupied disk space : ");
              values.append(String.format("%5.2f", Double.valueOf(root.getTotalSpace() - root.getFreeSpace())/1024/1024/1024));
              values.append(" Gb\n\n");

          }
        }

    mainWindow.pack();              //Creating the window
    mainWindow.setLocationRelativeTo(null);         //true: position at (0,0)     false: position at center
    mainWindow.setResizable(false);         //Intuitively known commands
    mainWindow.setVisible(true);        //Intuitively known commands
}

public static void main(String[] args) 
{
    new JavaBenchmark("Display");
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==exit) 
    {
        System.exit(0);
    }
}
}

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

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

发布评论

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

评论(2

找回味觉 2024-12-29 07:25:52

JavaBenchmark$1.class 是您在添加窗口侦听器时为 WindowAdapter 创建的匿名类。

至于 Jar 不起作用,您必须描述您正在做什么来创建 Jar 以确定哪里出了问题。很可能您只是发出了错误的命令。

The JavaBenchmark$1.class is an anonymous class you create for WindowAdapter when you add a window listener.

As far as the Jar not working, you'd have to describe what you're doing to create the Jar to identify where you're going wrong. Chances are you're simply issuing the wrong command.

飘然心甜 2024-12-29 07:25:52

JavaBenchmark$1.class 是匿名类,定义如下:

new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}}

JavaBenchmark$1.class is the anonymous class defined as follows:

new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文