Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1157942
  • 博文数量: 146
  • 博客积分: 6619
  • 博客等级: 准将
  • 技术积分: 1621
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-29 14:06
文章分类

全部博文(146)

文章存档

2020年(1)

2019年(4)

2018年(3)

2017年(5)

2015年(5)

2014年(7)

2013年(5)

2012年(11)

2011年(15)

2010年(13)

2009年(14)

2008年(63)

分类: Java

2014-11-14 13:07:33

打好jar包之后还需要更改清单文件的.

打开生成的jar,里面有一个MANIFEST.MF的文件把它打开.

然后有一行Main-Class,没有就加上。如:

Manifest-Version: 1.0
Main-Class: 类的全地址

 

要在后面加上你的主main class文件.比如你的文件是HelloWrold.java编译后就是HelloWrold.class.那么这里就写HelloWrold.然后回车,

 
Main-Class: com.HelloWrold

注意一定要在名字后面有一个回车让光标到下一行.

这样你生成的jar包才能找到你的主class去运行

用工具打包的时候最后一步,选择Main calss的路径地址。

例子:MANIFEST.MF

Manifest-Version: 1.0
Main-Class: com.MD5File


执行: java -jar getMD5File.jar c:\systeminfo.txt

 

代码如下:

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5File {

 protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

 // private ReadBufferBean readBufferBean;

 protected MessageDigest messagedigest = null;

 public MD5File() {
  try {
   messagedigest = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
   // log.error(MD5File.class.getName()+
   // "初始化失败,MessageDigest不支持MD5Util。", e);
   messagedigest = null;
  }
 }

 public void putContent(byte[] b) {
  putContent(b, 0, b.length);
 }

 public void putContent(byte[] b, int start, int length) {
  if (messagedigest != null)
   messagedigest.update(b, start, length);
 }

 public String bufferToHex() {
  String md5_value = bufferToHex(messagedigest.digest());
  return md5_value;
 }

 /**
  * 生成文件的md5校验值
  * 
  * @param file
  * @return
  * @throws IOException
  */
 public String getFileMD5String(File file) throws IOException {
  InputStream fis = null;
  try {
   fis = new FileInputStream(file);
   byte[] buffer = new byte[1024];
   int numRead = 0;
   while ((numRead = fis.read(buffer)) > 0) {
    messagedigest.update(buffer, 0, numRead);
   }
  } finally {
   if (fis != null)
    try {
     fis.close();
    } catch (IOException e) {
     // log.error("close error file:"+file.getAbsolutePath(),e);
    }
  }
  return bufferToHex(messagedigest.digest());
 }

 private String bufferToHex(byte bytes[]) {
  return bufferToHex(bytes, 0, bytes.length);
 }

 private String bufferToHex(byte bytes[], int m, int n) {
  StringBuffer stringbuffer = new StringBuffer(2 * n);
  int k = m + n;
  for (int l = m; l < k; l++) {
   appendHexPair(bytes[l], stringbuffer);
  }
  return stringbuffer.toString();
 }

 private void appendHexPair(byte bt, StringBuffer stringbuffer) {
  char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>
  // 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
  char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
  stringbuffer.append(c0);
  stringbuffer.append(c1);
 }

 public static void main(String[] args) throws IOException {
  long begin = System.currentTimeMillis();
  MD5File md5 = new MD5File();
//  File file = new File("c:\\systeminfo.txt");
   File file = new File(args[0]);
  String md5str = md5.getFileMD5String(file);
  System.out.println(md5str);

  // String md5 = getFileMD5String(file);

  // String md5 = getMD5String("a");

  long end = System.currentTimeMillis();
  System.out.println("md5:" + md5str + " time:" + ((end - begin) / 1000) + "s");
 }
}

阅读(28106) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~