java 程序到 linux

 

  • mave 打包 spring 模块的项目注意 rg.springframework.boot 插件的 <skip>true</skip> 选项,在打包给 linux 的时候要注释掉, 不能依赖不会打包,平时测试倒是没什么问题 🧡
  • 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("成功读取图片!");
      
      
          }
      }

       

posted @ 2025-02-08 19:53  封兴旺  阅读(12)  评论(0)    收藏  举报

联系方式: 18274305123(微信同号)