程序包com.sun.image.codec.jpeg不存在 的几种解决方案和遇到的坑

maven打包出现XXXX.java:[3,32] 程序包com.sun.image.codec.jpeg不存在

总结一下有几种解决方案:

1.不用jpeg这个类:

    ByteArrayOutputStream out = null;
        byte[] b = null;
        try {
            BufferedImage bi = ImageIO.read(is);
            Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
            BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
            thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
            
            out = new ByteArrayOutputStream();
            
            // 绘图
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
            param.setQuality(1.0f, false);
            encoder.encode(thumbnail);
            out.flush();
            b = out.toByteArray();
            out.close();
            bi.flush();
            bi = null;
        } catch (IOException e) {
            logger.error(Util.stackTrace2String(e));
        }finally{
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

2.想办法打包的时候包含进rt.jar

比如利用maven-compiler-plugin里面的bootclasspath:


                maven-compiler-plugin
                2.3.2
                
                    1.6
                    1.6
                    UTF-8
                    true  
                    true  
                    true  
                    false
                    
                        
                        ${java.home}/lib/rt.jar;${java.home}/lib/jce.jar
                    
                
            

3.打包的时候包含rt.jar的文件夹:


				org.apache.maven.plugins
				maven-compiler-plugin
				3.3
				
					1.7
					1.7
					utf8
					
						${env.JAVA_HOME}/jre/lib
					
				
			

4.你也可以把rt.jar放到webapp/WEB-INF/lib路径下面,然后就只要:


				org.apache.maven.plugins
				maven-compiler-plugin
				3.3
				
					1.7
					1.7
					utf8
					
						${basedir}/src/main/webapp/WEB-INF/lib
					
				
			

5.遇到的坑:

A.两个标签,如果配置多个数据,mac,linux用冒号(:),而windows用分号(;)

B.两个标签,windows路径用\,mac,linux用/


参考:

https://www.cnblogs.com/sagech/p/5025412.html

你可能感兴趣的:(坑)