我的 Jar 文件都没有运行。我创建了一个在 Netbeans 中运行良好的项目,但 Jar 没有显示任何内容。通过cmd运行它会显示noclassdeffounderror等。我创建了一个简单的项目,其中包含一个将 JFrame 设置为可见的类,该类仅显示"我的第一个应用程序",JFrame 具有绝对布局,可在 Netbeans 项目中添加库,但 jar 没有该库,因此会引发错误
package app;
import app.Look;
public class App
{
public static void main(String[] args)
{
System.out.println("Run");
Look n = new Look();
n.setVisible(true);
}
}
和 JFrame 文件代码
package app;
public class Look extends javax.swing.JFrame {
public Look() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("MY FIRST APP");
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 140, 230, 70));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 730, 340));
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new Look().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
线程"AWT-EventQueue-0"中的异常 java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout 在应用程序。Look.initComponents(Look.java:15( ....
我用你的文件做了一个 Netbeans 项目,一切正常。以下是步骤。
首先,创建一个新的 Java 项目,添加两个文件并根据以下说明导入正确的库(也许您已经这样做了(:
https://www.youtube.com/watch?v=B0prYbtdKNI
然后通过"清理并构建"构建项目。在项目级别,您将获得一个包含以下内容的"dist"文件夹:
dist/lib/AbsoluteLayout.jar
dist/README.TXT
dist/StackOverflow1.jar
我将我的项目命名为"Stackoverflow1"。因此,要从命令行运行它,您可以执行以下操作:
cd <project_dir>/dist
java -jar StackOverflow1.jar
如果你想移动.jar,例如将其移动到一个目录上方,你可以通过以下方式运行它:
cd <project_dir>
copy dist/StackOverflow1.jar ./
java -cp dist/lib/AbsoluteLayout.jar;StackOverflow1.jar app.App
您不能通过以下方式执行此操作,这将为您提供报告的错误
java -cp dist/lib/AbsoluteLayout.jar -jar StackOverflow1.jar
诀窍是,如果使用 -jar 选项,则类路径将仅通过 StackOverflow1.jar 清单文件中的条目"类路径:"定义。因此.jar为了从另一个位置使用 AbsoluteLayout,您必须将 AbsoluteLayout.jar 和 StackOverflow1.jar 放在类路径中,然后通过调用主类来调用您的应用程序。在此处查看更多内容
"java -cp"和"java -jar"之间的区别?