You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CN1 Legacy转Maven迁移后图片资源无法识别问题求助

问题:Codename One项目从Legacy迁移至Maven架构后图片无法识别

迁移后图片存放路径为\common\src\main\resources,迁移前直接存放于src目录,当前获取图片的代码如下:

第一处代码:

super(new BorderLayout());
try {
    add(BorderLayout.CENTER, new ScaleImageLabel(Image.createImage("/Logo_3.png")));
}    

第二处代码:

EncodedImage logo1 = EncodedImage.create("/Logo_1.png");
topLogo = new ScaleImageLabel(logo1);
topLogo.setUIID("CompanyLogoTab");    

解决方法

  • 修正资源路径写法
    Maven架构下,src/main/resources下的文件会被打包到classpath根目录,代码里不需要在文件名前加/,直接使用文件名即可。修改后的代码如下:

    // 第一处修改后
    super(new BorderLayout());
    try {
        add(BorderLayout.CENTER, new ScaleImageLabel(Image.createImage("Logo_3.png")));
    }    
    
    // 第二处修改后
    EncodedImage logo1 = EncodedImage.create("Logo_1.png");
    topLogo = new ScaleImageLabel(logo1);
    topLogo.setUIID("CompanyLogoTab");    
    

    同时要确保图片文件名大小写和代码中的完全一致,Codename One对资源文件名大小写敏感。

  • 验证Maven资源配置
    检查项目pom.xml的资源插件配置,确保没有排除图片文件。如果有自定义配置,要包含所有图片格式:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.png</include>
                    <include>**/*.jpg</include>
                    <include>**/*.gif</include>
                    <!-- 按需添加其他图片格式 -->
                </includes>
            </resource>
        </resources>
    </build>
    
  • 清理并重新构建项目
    执行Maven命令清理旧构建文件并重新打包,确保资源文件被正确复制:

    mvn clean install
    

    也可以在IDE中执行「Clean」和「Build」操作完成同样的步骤。

内容的提问来源于stack exchange,提问作者user1678742

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 14:02:37