Java在Linux平台编译出错,都是rt.jar惹的祸

给公司项目做集成,发现项目在windows平台下能编译,在linux平台下编译报错。
原因是:<bootclasspath>${java.home}\lib\rt.jar</bootclasspath> 中的 "\" ,在linux平台不能编译。 把它改成 “/”, 其它开发人员用的 Eclipse会出错。
最好的解决办法是不用这个“rt.jar”,(本人看到别人项目引用这个包,会感觉项目特别重量)。
删除这个包,发现只有一个文件报错,程序包com.sun.image.codec.jpeg不存在。 一个验证码图片相关类用到了这个文件。



<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}\lib\rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>


解决办法(转载)
转载:http://superich2008.iteye.com/blog/2047830

失败提示信息为:程序包com.sun.image.codec.jpeg不存在
这个类文件的位置在jre/lib/rt.jar

而我们设置的java_home下面的lib/dt.jar中没有这个文件,导致编译失败。通过配置maven-compiler-plugin插件可以解决此问题。



复制代码

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<optimize>true</optimize>
<debug>true</debug>
<showDeprecation>true</showDeprecation>
<showWarnings>false</showWarnings>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>

复制代码



在windows下面用;分隔,linux下面用:分隔。

方案-:

  查询网上的解决方案,但是仍然报编译失败。后经过查找,最终定位问题。

  原因是由于编译的依赖JDK版本过高引起的。从JDK1.7开始,中com.sun.image.codec.jpeg这个类被删除,所以编译总是报错,解决方案,编译的JDK版本设置为JDK1.6或者以下版本,编译通过。

方案二:

  解决代码API引用问题。

  原始代码:

复制代码

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();
}
}
}

复制代码
改变实现方式:

复制代码

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);

ImageIO.write(bi, "jpg", out);

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();
}
}
}

复制代码
即可解决报错问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值