- maven 打包为 .jar 包, 然后 linux 上运行 java -jar modules_1-1.0-SNAPSHOT.jar
需要指定主类
<build><plugins><!-- 为了在 linux 上使用 java -jar modules_1-1.0-SNAPSHOT.jar 正常运行, 添加 maven-jar-plugin 插件 指定主类 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.3.0</version><configuration><archive><manifest><addClasspath>true</addClasspath><mainClass>HelloWord</mainClass> <!-- 指定主类 --></manifest></archive></configuration></plugin></plugins></build>
- linux 的文件分隔符和 window 上不一样 可以用 String path = "a" + File.separator + "b"; 来区分 以下是一个简单的实例:
-
public class HelloWord {public static void main(String[] args) {System.out.println("Hello Maven ...");String path = "folder" + File.separator + "subfolder"; // File.separator 就是 文件路径的斜杠System.out.println(path);String osName = System.getProperty("os.name").toLowerCase();String imgPath = "";if (osName.contains("win")) {imgPath = "D:\\Av\\Img\\wallpaper";} else {imgPath = "/media/img/1-test";}imgPath += File.separator + "1.jpg";System.out.println( imgPath);File input = new File(imgPath);BufferedImage image = null;try {image = ImageIO.read(input);} catch (IOException e) {throw new RuntimeException(e);}System.out.println("图片宽度:" + image.getWidth());System.out.println("图片高度:" + image.getHeight());System.out.println("成功读取图片!");} }
-