gpt4 book ai didi

java - .jar 不会显示

转载 作者:行者123 更新时间:2023-12-02 07:56:46 24 4
gpt4 key购买 nike

好吧,由于某种原因,我的 .jar 不会执行,即使它在 Eclipse 中执行。这是我的代码,它不是最好的,但是我正在尝试。我需要一些帮助来让它作为 .jar 文件在 eclipse 之外执行。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar; // only need this one class
import javax.swing.*;
////////////////////////////////////////////////////////////////// TextClock
public class CopyOftheclock {
//================================================================= main
public static void main(String[] args) {
JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}//end main
}//endclass TextClock


@SuppressWarnings("serial")
//////////////////////////////////////////////////////////// TextClockWindow
class TextClockWindow extends JFrame {
//=================================================== instance variables
private JTextField timeField; // set by timer listener

//========================================================== constructor
public TextClockWindow() {
// Build the GUI - only one panel
timeField = new JTextField(7);
timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

Container content = this.getContentPane();
content.setLayout(new FlowLayout());
content.add(timeField);

this.setTitle("Norway");
this.pack();

// Create a 1-second timer and action listener for it.
// Specify package because there are two Timer classes
javax.swing.Timer t = new javax.swing.Timer(1000,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a = "";
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
if (h==24)
{
h=8;
a = "A.M";
}
if (h==1)
{
h=9;
a = "A.M";
}
if (h==2)
{
h=10;
a = "A.M";
}
if (h==3)
{
h=11;
a = "A.M";
}
if (h==4)
{
h=12;
a = "P.M";
}
if (h==5)
{
h=1;
a = "P.M";
}
if (h==6)
{
h=2;
a = "P.M";
}
if (h==7)
{
h=3;
a = "P.M";
}
if (h==8)
{
h=4;
a = "P.M";
}
if (h==9)
{
h=5;
a = "P.M";
}
if (h==10)
{
h=6;
a = "P.M";
}
if (h==11)
{
h=7;
a = "P.M";
}
if (h==12)
{
h=8;
a = "P.M";
}
if (h==13)
{
h=9;
a = "P.M";
}
if (h==14)
{
h=10;
a = "P.M";
}
if (h==15)
{
h=11;
a = "P.M";
}
if (h==16)
{
h=12;
a = "P.M";
}
if (h==17)
{
h=1;
a = "A.M";
}
if (h==18)
{
h=2;
a = "A.M";
}
if (h==19)
{
h=3;
a = "A.M";
}
if (h==20)
{
h=4;
a = "A.M";
}
if (h==21)
{
h=5;
a = "A.M";
}
if (h==22)
{
h=6;
a = "A.M";
}
if (h==23)
{
h=7;
a = "A.M";
}
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
timeField.setText("" + h + ":" + m + ":" + s + " " + a);
}

});
t.start(); // Start the timer
}//end constructor
}//endclass TextClock

最佳答案

制作 jar

导出为可运行的 jar

enter image description here enter image description here

如果您成功制作了 jar ,请执行下一步。

从控制台运行它

方法1

java [-选项] -jar jarfile [args...]

注意:其中[-options]是JVM的参数,[args...]是你的jar的参数

此外,只有当您的 jar list 具有如下所示的主类条目时,这才有效:

Manifest-Version: 1.0
Class-Path: .
Main-Class: CopyOftheclock

方法2

像这样设置你的类路径:

set classpath=clock.jar;.;%classpath%

在此之后:

java [-options] class [args...]

其中 class 是带有 main 方法的类。另外,class 应该是完全限定的,即如果它位于package a.b.c 中,那么该类应该是a.b.c.CopyOftheclock。另外,您应该从最顶层的父包触发 java 命令。

方法3

我不确定基于 *nix 的操作系统,但在 Windows 上只需右键单击 jar 并选择使用 javajavaw 运行。但为此, list 文件必须具有 main class 条目,否则会失败。

注意:有关[选项]的更多详细信息,请参阅java -help

关于java - .jar 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com