How to build an execute jar file from java code without framework

What troubled me a lot is ‘no main manifest attribute, in XXX.jar’. In the following, I’ll record the jar packaging process.

As most of as know that we may use ‘java cvf’ command to get the goal packaging one execute jar. For example:

  • Prepare one java source code including main function entrance.

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class DesEncryption {

  public static void main(String[] argv) {

    try {

      KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
      SecretKey myDesKey = keygenerator.generateKey();

      Cipher desCipher;

      // Create the cipher
      desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

      // Initialize the cipher for encryption
      desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

      //sensitive information
      byte[] text = "No body can see me".getBytes();

      System.out.println("Text [Byte Format] : " + text);
      System.out.println("Text : " + new String(text));

      // Encrypt the text
      byte[] textEncrypted = desCipher.doFinal(text);

      System.out.println("Text Encryted : " + textEncrypted);

      // Initialize the same cipher for decryption
      desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

      // Decrypt the text
      byte[] textDecrypted = desCipher.doFinal(textEncrypted);

      System.out.println("Text Decryted : " + new String(textDecrypted));

    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }

  }

}

 

  • Details about the directory

.
├── DesEncryption.class
└── DesEncryption.java

 

  • Execute the compiled codes.

ivan@ivan java DesEncryption

output:

Text [Byte Format] : [B@73035e27
Text : No body can see me
Text Encryted : [B@64c64813
Text Decryted : No body can see me

As we see, the code without mistakes and main function entrance works good.

  • Do jar packaging and exeute the jar

ivan@ivan jar cvf DesEncryption.jar .
ivan@ivan java -jar DesEncryption.jar

Comes the confusing error message like:

no main manifest attribute, in DesEncryption.jar

After accessing some documents, mainly from this page: https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute

  • Provide META-INF folder and tell where is the main function entrance.

├── DesEncryption.class
├── DesEncryption.java
└── META-INF
└── MANIFEST.MF

Look into the details about MANIFEST.MF.


ivan@ivan cat META-INF/MANIFEST.MF

Main-Class: DesEncryption

  • Jar packaging again and execute the jar.

ivan@ivan jar cmvf META-INF/MANIFEST.MF DesEncryption.jar .
ivan@ivan java -jar DesEncryption.jar

Succeed and comes the output:

Text [Byte Format] : [B@445b84c0
Text : No body can see me
Text Encryted : [B@61a52fbd
Text Decryted : No body can see me

 

 

 

 

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>