我通过谷歌和各种论坛进行了搜索,(也是这个)。
我创建了一个应用程序,一个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);
}
}
}
答案 0 :(得分:1)
JavaBenchmark$1.class
是您在添加窗口侦听器时为WindowAdapter
创建的匿名类。
就Jar不起作用而言,你必须描述你正在做什么来创建Jar来识别你出错的地方。你可能只是发出了错误的命令。
答案 1 :(得分:1)
JavaBenchmark $ 1.class是匿名类,定义如下:
new WindowAdapter(){public void windowClosing(WindowEvent we){System.exit(0);}}