Java导出为Jar有错误 [英] Java Exporting as Jar has errors
问题描述
两个问题
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;
按钮btnNext;
按钮btnDone;
文本输入;
文本输出;
static int count;
static int [] finalNums;
int [] nums = new int [1000];
public static void main(String [] args){
显示display = new Display();
new UserInterface(display);
display.dispose();
}
public UserInterface(显示显示){
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(完成);
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)+\\\
这些数字没有\\\
common乘数);
}
else {
output.setText(Arrays .toString(finalNums)+\\\
Result:+ String.valueOf(commonDenom.getResult()));
}
}
});
}
public static int [] getNums(){
return finalNums;
}
}
Manifest.txt位置:/ Dropbox / workspace / commonDenom / bin
类位置:/ Dropbox / workspace / commonDenom / bin / commonDenom /
类名: / p>
commonDenom.class
UserInterface.class
UserInterface $ 1.class(我没有创建这个)
UserInterface $ 2.class(我没有创建这个)
Manifest.txt内容主要类别:commonDenom.UserInterface
pre>
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。这将是一个很好的理性测试,你将会学习做什么。以下是步骤:
-
在某个地方创建一个
Manifest.txt
让我们说你的项目根。一行就足够了,如下所示:主类:commonDenom.UserInterface
确保文件以换行符结尾。默认情况下,Eclipse不会在文件末尾添加换行符。没有空白行。
-
转到Eclipse放置您的类文件的父目录。例如在Maven项目中,这是项目根目录中的
target / classes
relative。 Ant构建可能会使用bin
。在你的例子中,这应该是包含commonDenom
(其又包含构建产品:UserInterface.class
)的目录) -
创建jar:
jar cfm / tmp /somename.jar /path/to/Manifest.txt commonDenom
#或大概:
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
...#你的其他类文件...
现在你已经有了基准测试您可以尝试使用Eclipse创建该jar。如果Eclipse创建的jar不起作用,您可以查看其内容以查看与基准情况的差异,这将有助于您调试问题。
Two questions
1) When exporting a java project as a JAR file, how does the application know which Class in the package to run first? My applicatino specifically requires that the userInterface.java file run before the CommonDenom.java file.
2) When running the java file I'm getting an error that says "The Java JAR file "commonDenom.jar" could not be launched. Check the Console for possible messages."
Where do I start to figure this out? I checked the console but it doesn't seem to be registering anything at the time the error message pops.
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 location: /Dropbox/workspace/commonDenom/bin
Class location: /Dropbox/workspace/commonDenom/bin/commonDenom/
Class names:
commonDenom.class
UserInterface.class
UserInterface$1.class (I didn't create this)
UserInterface$2.class (I didn't create this)
Manifest.txt content (with two trailing blank lines):
Main-Class: commonDenom.UserInterface
jar tf CommonDenom.jar returns the following:
META-INF/
META-INF/MANIFEST.MF
Manifest.txt
commonDenom/
commonDenom/commonDenom.class
commonDenom/UserInterface$1.class
commonDenom/UserInterface$2.class
commonDenom/UserInterface.class
I recommend to create the jar by hand first. It will be a good sanity test, and you will learn what it takes to do it. Here are the steps:
Create a
Manifest.txt
file somewhere, let's say your project root. One line is enough, like this:Main-Class: commonDenom.UserInterface
Make sure the file ends with a newline. Eclipse doesn't put a newline at the end of the file by default. It's ok to have trailing blank lines.
Go to the parent directory where Eclipse puts your class files. For example in a Maven project this is
target/classes
relative from the project root. Ant builds might usebin
instead. In your example this should be the directory that containscommonDenom
(which in turn contains the build product:UserInterface.class
)Create the jar:
jar cfm /tmp/somename.jar /path/to/Manifest.txt commonDenom # or probably: jar cfm /tmp/somename.jar /path/to/Manifest.txt *
This will put your tree of class files into the jar with the specified manifest file.
Now you should be able to run this file. This can be your baseline test.
You can check the content of the jar with the command:
jar tf /tmp/somename.jar
It should print something like:
META-INF/
META-INF/MANIFEST.MF
commonDenom/UserInterface.class
... # your other class files...
Now that you have your baseline test, you can try to create the jar using Eclipse. If the jar created by Eclipse doesn't work, you can look at its content to see the differences from your baseline case, which should help you debug the problem.
这篇关于Java导出为Jar有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!