Java导出为Jar有错误

两个问题

1)将java项目导出为JAR文件时,应用程序如何知道首先运行包中的哪个Class? 我的applicatino特别要求userInterface.java文件在CommonDenom.java文件之前运行。

2)运行java文件时,我收到一条错误,上面写着“Java JAR文件”commonDenom.jar“无法启动。检查控制台是否有可能的消息。”

我从哪里开始想出这个? 我检查了控制台,但它似乎没有在错误消息弹出时注册任何内容。

package commonDenom; import java.util.Arrays; import java.util.Scanner; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class UserInterface { Shell shell; Button btnNext; Button btnDone; Text input; Text output; static int count; static int[] finalNums; int[] nums = new int[1000]; public static void main(String[] args){ Display display = new Display(); new UserInterface(display); display.dispose(); } public UserInterface(Display display){ shell = new Shell(display); shell.setSize(220,350); shell.open(); input = new Text(shell, SWT.SINGLE); input.setBounds(10, 10, 100, 20); btnNext = new Button(shell, SWT.PUSH); btnNext.setBounds(10, 40, 100, 30); btnNext.setText("Next"); nextPress(); btnDone = new Button(shell, SWT.PUSH); btnDone.setBounds(10, 80, 100, 30); btnDone.setText("Done"); donePress(); output = new Text(shell, SWT.SINGLE); output.setBounds(10, 120, 200, 200); while(!shell.isDisposed()){ if(!display.readAndDispatch()){ display.sleep(); } } } public void nextPress(){ btnNext.addSelectionListener(new SelectionAdapter(){ int x = 0; @Override public void widgetSelected(SelectionEvent e) { nums[x] = Integer.parseInt(input.getText()); System.out.println("nums[" + x + "]:" + nums[x]); x++; count++; } }); } public void donePress(){ btnDone.addSelectionListener(new SelectionAdapter(){ @Override public void widgetSelected(SelectionEvent e) { finalNums = new int[count]; for(int i = 0; i < count; i++){ finalNums[i] = nums[i]; } System.out.println("finalNums:" + Arrays.toString(finalNums)); commonDenom.compare(); if(commonDenom.getResult() == 0){ output.setText(Arrays.toString(finalNums) + "\nThese numbers do not have a \ncommon multiplier"); } else{ output.setText(Arrays.toString(finalNums) + "\nResult:" + String.valueOf(commonDenom.getResult())); } } }); } public static int[] getNums(){ return finalNums; } } 

Manifest.txt位置:/ Dropbox / workspace / commonDenom / bin

class级位置:/ Dropbox / workspace / commonDenom / bin / commonDenom /

class级名称:

 commonDenom.class UserInterface.class UserInterface$1.class (I didn't create this) UserInterface$2.class (I didn't create this) 

Manifest.txt内容(带有两个尾随空白行):

 Main-Class: commonDenom.UserInterface 

jar tf CommonDenom.jar返回以下内容:

 META-INF/ META-INF/MANIFEST.MF Manifest.txt commonDenom/ commonDenom/commonDenom.class commonDenom/UserInterface$1.class commonDenom/UserInterface$2.class commonDenom/UserInterface.class 

我建议先手工制作jar子。 这将是一个很好的理智测试,你将学习如何做到这一点。 以下是步骤:

  1. 在某个地方创建一个Manifest.txt文件,让我们说你的项目根目录。 一行足够了,像这样:

     Main-Class: commonDenom.UserInterface 

    确保文件以换行符结尾。 默认情况下,Eclipse不会在文件末尾添加换行符。 尾随空白行是可以的。

  2. 转到Eclipse放置类文件的父目录 。 例如,在Maven项目中,这是相对于项目根目录的target/classes 。 Ant构建可能会使用bin 。 在您的示例中,这应该是包含commonDenom的目录(其中包含构建产品: UserInterface.class

  3. 创建jar:

     jar cfm /tmp/somename.jar /path/to/Manifest.txt commonDenom # or probably: jar cfm /tmp/somename.jar /path/to/Manifest.txt * 

    这会将您的类文件树放入具有指定清单文件的jar中。

现在您应该能够运行此文件。 这可以是您的基线测试。

您可以使用以下命令检查jar的内容:

 jar tf /tmp/somename.jar 

它应该打印如下:

 META-INF/ META-INF/MANIFEST.MF commonDenom/UserInterface.class ... # your other class files... 

现在您已经进行了基线测试,您可以尝试使用Eclipse创建jar。 如果Eclipse创建的jar不起作用,您可以查看其内容以查看与基线情况的差异,这可以帮助您调试问题。

您可以在MANIFEST文件中定义应用程序的入口点。 看看这里: http : //docs.oracle.com/javase/tutorial/deployment/jar/appman.html