Jar不能在命令行上运行

时间:2021-04-11 13:29:00

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.

我创建了一个应用程序,一个java基准测试,并且想要创建一个可运行的jar文件,以便在其他机器上使用该程序。不幸的是,jar没有工作,一切都完成了使用jar文件的代码,程序在命令行上运行。我尝试在这个论坛上找到的技巧来修复我的jar创建,但它没有用。

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?)

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

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

所以我要求你检查我的代码是否存在一些问题,我必须说它是一个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);
    }
}
}

2 个解决方案

#1


1  

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

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

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.

至于Jar不工作,你必须描述你正在做什么来创建Jar来识别你出错的地方。你可能只是发出了错误的命令。

#2


1  

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

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

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

#1


1  

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

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

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.

至于Jar不工作,你必须描述你正在做什么来创建Jar来识别你出错的地方。你可能只是发出了错误的命令。

#2


1  

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

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

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