英文:
Cannot create and run a JAR file from powershell
问题
以下是翻译好的内容:
我无法使用 PowerShell 创建可执行的 JAR 文件。一直出现的错误要么是“无效的头字段”要么是“找不到或加载主类”。我已经成功地从 Eclipse 中运行了代码,所以我知道问题不在代码上(尽管我也愿意接受一些讽刺)。这是我的目录结构:
...Documents/Java/drawingFX
在 drawingFX 目录中有文件“Drawing.java”、“JIGraphicsUtility.java”和“Manifest.txt”。
我现在使用以下命令在 drawingFX 目录中使用 PowerShell 编译 .java 文件:
PS C:\Users\colin\Documents\Java\drawingFX> javac --module-path "C:\Program Files\Java\jdk-14.0.1\package\javafx-sdk-14.0.1\lib" --add-modules javafx.controls,javafx.fxml Drawing.java JIGraphicsUtility.java
“module-path” 部分是为了包括一些 JavaFX 包的 JAR 文件。现在编译完成后,drawingFX 目录中有两个更多的文件:“Drawing.class” 和 “JIGraphicsUtility.class”。现在我尝试创建 JAR 文件(我将提到 “main” 在 “Drawing.class” 中)使用以下命令在 drawingFX 目录中的 PowerShell:
PS C:\Users\colin\Documents\Java\drawingFX> jar cvfm run.jar Manifest.txt *.class
我收到一个 “无效的头字段” 错误。现在我将为所有文件包括代码,然后将包括我的 CLASSPATH 的目录。
以下是 “Drawing.java” 的代码:
/*
文件:Drawing.java
作者:Colin Rubow
最后修改日期:2020 年 5 月 27 日
描述:此程序将与绘图 JavaFX 应用程序一起使用
*/
package drawingFX;
import javafx.application.Application;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;
public class Drawing extends Application {
@Override
public void start(Stage stage) {
final int WIDTH = 700;
final int HEIGHT = 400;
GraphicsContext gc = JIGraphicsUtility.setUpGraphics(stage, "Drawing", WIDTH, HEIGHT);
gc.strokeLine(100, 125, 100, 300);
gc.setStroke(Color.RED);
gc.setLineWidth(5);
gc.strokeLine(300, 75, 550, 75);
Random rand = new Random();
int red = rand.nextInt(0xFF);
int green = rand.nextInt(0xFF);
int blue = rand.nextInt(0xff);
Color randomColor = Color.rgb(red, green, blue);
gc.setStroke(randomColor);
gc.strokeLine(0, 0, WIDTH - 1, HEIGHT - 1);
}
public static void main(String[] args) {
launch(args);
}
}
以下代码来自 “JIGraphicsUtility.java”:
/*
*/
package drawingFX;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JIGraphicsUtility {
public static GraphicsContext setUpGraphics(Stage stage, String title, int height, int width) {
stage.setTitle(title);
Canvas canvas = new Canvas(height, width);
GraphicsContext gc = canvas.getGraphicsContext2D();
Group root = new Group(canvas);
stage.setScene(new Scene(root));
stage.show();
return gc;
}
}
以下是 “Manifest.txt” 的内容:
Main-Class: drawingFX.Drawing
以下是 CLASSPATH 中的内容:
C:\Program Files\Java\jdk-14.0.1\package\javafx-sdk-14.0.1\lib
C:\Users\colin\Documents\Java\drawingFX
C:\Users\colin\Documents\Java
我认为当前的 “无效的头字段” 错误是由于 “Manifest.txt” 文件引起的,但我不确定。至于 “找不到或加载主类” 错误,我毫无头绪,因为我已经尝试了许多方法。
提前谢谢。
英文:
I have been unable to create an executable JAR file using the powershell. The error that keeps coming up is either "invalid header field" or "Could not find or load main class". I have successfully ran the code from Eclipse so I know it's not the code (I'm open for irony though). This is what my directory looks like:
...Documents/Java/drawingFX
inside drawingFX are the files "Drawing.java", "JIGraphicsUtility.java", and "Manifest.txt"
I now compile the .java file using the following command in powershell in the drawingFX directory where the files are:
PS C:\Users\colin\Documents\Java\drawingFX> javac --module-path "C:\Program Files\Java\jdk-14.0.1\package\javafx-sdk-14.0.1\lib" --add-modules javafx.controls,javafx.fxml Drawing.java JIGraphicsUtility.java
the "module-path" part is to include some JAR files for the javafx package stuff. Now that I have compiled there are two more files in drawingFX: "Drawing.class" and "JIGraphicsUtility.class". Now I attempt to create the JAR file (I will mention that 'main' is located in Drawing.class") using the following command in powershell in the drawingFX directory:
PS C:\Users\colin\Documents\Java\drawingFX> jar cvfm run.jar Manifest.txt *.class
I get an "invalid header field" error. I will now include the code for all the files and then will include the directories of my CLASSPATH.
The following is the code for "Drawing.java":
/*
file:: Drawing.java
author:: Colin Rubow
last date modified:: 5/27/2020
description:: this program will play with the drawing javafx application
*/
package drawingFX;
import javafx.application.Application;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;
public class Drawing extends Application
{
@Override
public void start( Stage stage )
{
final int WIDTH = 700;
final int HEIGHT = 400;
GraphicsContext gc = JIGraphicsUtility.setUpGraphics( stage, "Drawing", WIDTH, HEIGHT );
gc.strokeLine( 100, 125, 100, 300 );
gc.setStroke( Color.RED );
gc.setLineWidth( 5 );
gc.strokeLine( 300, 75, 550, 75);
Random rand = new Random();
int red = rand.nextInt( 0xFF );
int green = rand.nextInt ( 0xFF );
int blue = rand.nextInt ( 0xff );
Color randomColor = Color.rgb( red, green, blue );
gc.setStroke( randomColor );
gc.strokeLine( 0, 0, WIDTH - 1, HEIGHT - 1 );
}
public static void main( String [] args )
{
launch( args );
}
}
The following code is from the "JIGraphicsUtility.java":
/*
*/
package drawingFX;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JIGraphicsUtility
{
public static GraphicsContext setUpGraphics( Stage stage, String title, int height, int width )
{
stage.setTitle( title );
Canvas canvas = new Canvas( height, width );
GraphicsContext gc = canvas.getGraphicsContext2D( );
Group root = new Group( canvas );
stage.setScene( new Scene( root ) );
stage.show( );
return gc;
}
}
The following is from the "Manifest.txt":
Main-Class: drawingFX.Drawing
The following are what is in CLASSPATH:
C:\Program Files\Java\jdk-14.0.1\package\javafx-sdk-14.0.1\lib
C:\Users\colin\Documents\Java\drawingFX
C:\Users\colin\Documents\Java
I believe the current "invalid header field" error is due to the Manifest.txt file but I do not know. As for the "Could not find or load main class" error I have no idea as I have tried many things.
Thank you in advance.
专注分享java语言的经验与见解,让所有开发者获益!
评论