我正在开始从 java 8 迁移到 java 16。我正在尝试使用 ModulePath 而不是 ClassPath。为了测试迁移,我在 com.test.main 中创建了一个只有一个类 (Main.java) 的简单新项目。我也需要 javafx 库。我的 module-info.java 进行导入:
open module com.test.main {
requires java.desktop;
requires javafx.graphics;
requires javafx.swing;
requires javafx.fxml;
requires javafx.controls;
}
你可以在这里看到我的项目目录
我使用 ant 来构建我的项目,所以我尝试对其进行调整。
<project name="Test" basedir=".">
<description>
build the test project
</description>
<!-- set global properties for this build -->
<property name="app.name" value="Test" />
<property name="VERSION" value="1.0" />
<property name="RELEASE" value="0" />
<property name="debug" value="false" />
<property name="optimize" value="true" />
<property name="deprecation" value="true" />
<property name="src" location=".."/>
<property name="build" location="../bin"/>
<property name="classdir" location="../bin/classes"/>
<property name="dist" location="../jlib"/>
<property name="package.dir" location="../packaging"/>
<property name="images_dist" location="../bin/classes/images"/>
<property name="external_components.location" value="../external" />
<!-- JavaFx -->
<path id="javafx.path">
<fileset dir="${external_components.location}/javafx-sdk-16">
<!-- catch all jar files in the current directory -->
<include name="**/*.jar" />
</fileset>
</path>
<!-- convert path to property -->
<pathconvert property="javaFxPath" refid="javafx.path" />
<!-- ModulePath -->
<path id="module.path">
<pathelement path="${javaFxPath}" />
</path>
<!-- convert path to property -->
<pathconvert property="modulePath" refid="module.path" />
<!-- general Init-->
<target name="init" depends="clean">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
<mkdir dir="${classdir}"/>
<mkdir dir="${dist}"/>
</target>
<!-- Compilation -->
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac includeantruntime="false" destdir="${classdir}" fork="yes" verbose="true" debug="${debug}" optimize="${optimize}" deprecation="${deprecation}" modulepath="${modulePath}" source="16">
<src path="${src}"/>
</javac>
</target>
<!-- Production of the JAR file -->
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/${app.name}.jar" basedir="${classdir}" >
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<!-- CLEAN -->
<target name="clean" description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete verbose="true" dir="${classdir}"/>
<delete verbose="true" dir="${build}"/>
</target>
当我运行它时,一切都很好,Ant 构建已正确完成。但是,当我尝试使用以下命令运行我的 jar 时:
java -jar .\jlib\Test.jar com.test.main.Main
我收到了这个错误:
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
有人知道我做错了什么吗?谢谢你的帮助。