2014-03-05 47 views
-1

有两个问题Java导出为Jar有错误

1)当将Java项目导出为JAR文件时,应用程序如何知道程序包中的哪个类首先运行?我的应用程序特别要求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位置:/升降梭箱/工作区/ commonDenom/bin中

类位置:/升降梭箱/工作区/ commonDenom/bin中/ commonDenom/

类名:

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 

罐子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 
+0

你的意思是_My applicatino(sic)特别要求userInterface.java文件在CommonDenom.java_之前运行?只有**一个**类被运行 - 在你的'META-INF/MANIFEST.MF'中被指定为'Main-Class'的类。有关更多信息,请参见[this](http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html)。附:你发布了**拼写错误**。这表明你很少或没有努力。如果你不能被打扰,为什么我们应该付出努力? –

回答

2

我建议先手动创建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 tf /tmp/somename.jar 

应该打印出类似这样:

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

现在,你有你的基准测试中,你可以尝试创建使用Eclipse的jar。如果由Eclipse创建的jar不起作用,您可以查看其内容以查看与基准案例的不同之处,这可以帮助您调试问题。

+1

我同意 - 请按照我之前的建议手动建立罐子并告诉我们会发生什么。 – jgitter

+0

在现在制作罐子的过程中。我把Commonifeenom中的Manifest.txt(与bin和src相同的目录)。然后你说要导航(我假设在终端中)到具有根项目commonDenom的bin文件夹。 (请注意,该项目命名为'commonDenom',该软件包也被称为'commonDenom')。所以现在在终端我的路径中显示'Stuart-Kuredjians-iMac-2:bin skuredjian $'因为'Manifest.txt'在与'bin'级别相同,我已经在'bin'中,当指定/ path/to时,如何备份一个级别? – dbconfession

+0

顺便说一句..我把清单INTO斌,它的工作。但是如果我希望舱单与舱位保持在同一水平而不是舱内呢?我不知道如何指定高于我所在的级别。 – dbconfession

1

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

+0

谢谢。我阅读了这份文件,这是我听到的第一份清单。这是哪里?它是一个文件吗?打包jar文件之前,是否可以在eclipse中指定入口点?或者我必须编辑一个实际的文本文件?谢谢。 – dbconfession

+0

不要紧,我在导出向导中找到它。让我试试看看它是否有效。默认情况下,我没有指定,打包工具将哪个类指定为入口点? – dbconfession

+0

这不起作用。我指定了正确的入口点类,但是我仍然得到“The Java JAR文件”commonDenom.jar“无法启动。 – dbconfession