Wednesday, September 9, 2015

How to run Java .jar without MANIFEST.MF?


Is it possible to run a Java app which doesn't contain MANIFEST.MF file? Of course, there's static main method,just lacks manifest file. And the app is depending on several external .jar files.
It is possible, you can specify the class to run from the command line:
java -cp yourJar.jar your.main.Class
Same question here:
shareimprove this answer
You can also add the manifest, with the following command:
jar  -uvfe  your.jar foo.bar.Baz
java -jar your.jar        # tries to run main in foo.bar.Baz
shareimprove this answer
Of course! Just use this:
java -cp MyJar.jar com.example.Main
shareimprove this answer
Yes , use this
java -cp myJar.jar package.className

Setting an Application's Entry Point

https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Setting an Application's Entry Point

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:
Main-Class: classname
The value classname is the name of the class that is your application's entry point.
Recall that the entry point is a class having a method with signature public static void main(String[] args).
After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:
java -jar JAR-name
The main method of the class specified in the Main-Class header is executed.

An Example

We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.
We first create a text file named Manifest.txt with the following contents:
Main-Class: MyPackage.MyClass

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: MyPackage.MyClass
When you run the JAR file with the following command, the main method of MyClass executes:
java -jar MyJar.jar

Setting an Entry Point with the JAR Tool

The 'e' flag (for 'entrypoint') creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:
jar cfe app.jar MyApp MyApp.class
You can directly invoke this application by running the following command:
java -jar app.jar
If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class

Monday, September 7, 2015

Java Keystore Tutorial


1. Introduction

Who of us didn’t visit ebay, amazon to buy anything or his personal bank account to check it. Do you think that those sites are secure enough to put your personal data like (credit card number or bank account number, etc.,)?
Most of those sites use the Socket Layer (SSL) protocol to secure their Internet applications. SSL allows the data from a client, such as a Web browser, to be encrypted prior to transmission so that someone trying to sniff the data is unable to decipher it.
Many Java application servers and Web servers support the use of keystores for SSL configuration. If you’re building secure Java programs, learning to build a keystore is the first step.

2. SSL and how it works

A HTTP-based SSL connection is always initiated by the client using a URL starting with https:// instead of with http://. At the beginning of an SSL session, an SSL handshake is performed. This handshake produces the cryptographic parameters of the session. A simplified overview of how the SSL handshake is processed is shown in the diagram below.
Java KeyStore Tutorial_html_m4e1c1e9d
This is in short how it works:
  1. A browser requests a secure page (usually https://).
  2. The web server sends its public key with its certificate.
  3. The browser checks that the certificate was issued by a trusted party (usually a trusted root CA), that the certificate is still valid and that the certificate is related to the site contacted.
  4. The browser then uses the public key, to encrypt a random symmetric encryption key and sends it to the server with the encrypted URL required as well as other encrypted http data.
  5. The web server decrypts the symmetric encryption key using its private key and uses the symmetric key to decrypt the URL and http data.
  6. The web server sends back the requested html document and http data encrypted with the symmetric key.
  7. The browser decrypts the http data and html document using the symmetric key and displays the information.
The world of SSL has, essentially, three types of certificates: private keys, public keys (also called public certificates or site certificates), and root certificates.

3. Private Keys

The private key contains the identity information of the server, along with a key value. It should keep this key safe and protected by password because it’s used to negotiate the hash during the handshake. It can be used by someone to decrypt the traffic and get your personal information. It like leaving your house key in the door lock.

4. Public Certificates

The public certificate (public key) is the portion that is presented to a client, it likes your personal passport when you show in the Airport. The public certificate, tightly associated to the private key, is created from the private key using a Certificate Signing Request (CSR). After you create a private key, you create a CSR, which is sent to your Certificate Authority (CA). The CA returns a signed certificate, which has information about the server identity and about the CA.

5. Root Certificates

Root CA Certificate is a CA Certificate which is simply a Self-signed Certificate. This certificate represents a entity which issues certificate and is known as Certificate Authority or the CA such as VeriSign, Thawte, etc.

6. Certificate Authorities

Companies who will sign certificates for you such as VeriSign, Thawte, Commodo, GetTrust. Also, many companies and institutions act as their own CA, either by building a complete implementation from scratch, or by using an open source option, such as OpenSSL.

7. Certificate Chain

When a server and client establish an SSL connection, a certificate is presented to the client; the client should determine whether to trust this certificate, a process called the certificate chain. The client examines the issuer of a certificate, searches its list of trusted root certificates, and compares the issuer on the presented certificate to the subjects of the trusted certificates.
If a match is found, the connection proceeds. If not, the Web browsers may pop up a dialog box, warning you that it cannot trust the certificate and offering the option to trust the certificate.

8. Keystore using Java keytool

Java Keytool is a key and certificate management utility. It allows users to manage their own public/private key pairs and certificates. Java Keytool stores the keys and certificates in what is called a keystore. It protects private keys with a password.
Each certificate in a Java keystore is associated with a unique alias. When creating a Java keystore you will first create the .jks file that will initially only contain the private key, then generate a CSR. Then you will import the certificate to the keystore including any root certificates.

9. Keystore Commands

Create Keystore, Keys and Certificate Requests
  • Generate a Java keystore and key pair
    1keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -storepass password
  • Generate a certificate signing request (CSR) for an existing Java keystore
    1keytool -certreq -alias mydomain -keystore keystore.jks -storepass password -filemydomain.csr
  • Generate a keystore and self-signed certificate 
    1keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360
Import Certificates
  • Import a root or intermediate CA certificate to an existing Java keystore
  • 1keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks -storepass password
  • Import a signed primary certificate to an existing Java keystore
    1keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
Export Certificates
  • Export a certificate from a keystore
    1keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks -storepass password
Check/List/View Certificates
  • Check a stand-alone certificate
    1keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore
    1keytool -list -v -keystore keystore.jks -storepass password
  • Check a particular keystore entry using an alias
    1keytool -list -v -keystore keystore.jks -storepass password -alias mydomain
Delete Certificates
  • Delete a certificate from a Java Keytool keystore
    1keytool -delete -alias mydomain -keystore keystore.jks -storepass password
Change Passwords
  • Change a Java keystore password
    1keytool -storepasswd -new new_storepass -keystore keystore.jks -storepass password
  • Change a private key password
    1keytool -keypasswd -alias client -keypass old_password -new new_password -keystore client.jks -storepass password

10. Configure SSL using Keystores and Self Signed Certificates on Apache Tomcat

  1. Generate new keystore and self-signed certificateusing this command, you will prompt to enter specific information such as user name, organization unit, company and location.
    1keytool -genkey -alias tomcat -keyalg RSA -keystore /home/ashraf/Desktop/JavaCodeGeek/keystore.jks -validity 360
    Java KeyStore Tutorial_html_m5d3841d
  2. You can list the certificate details you just created using this command
    1keytool -list -keystore /home/ashraf/Desktop/JavaCodeGeek/keystore.jks
    Java KeyStore Tutorial_html_131ca506
  3. Download Tomcat 7
  4. Configure Tomcat’s server to support for SSL or https connection. Adding a connector element in Tomcat\conf\server.xml
    1<Connector port="8443" maxThreads="150" scheme="https" secure="true"
    2SSLEnabled="true" keystoreFile="/home/ashraf/Desktop/JavaCodeGeek/.keystore"keystorePass="password" clientAuth="false" keyAlias="tomcat" sslProtocol="TLS" />
  5. Start Tomcat and go tohttps://localhost:8443/, you will find the following security issue where the browser will present untrusted error messages. In the case of e-commerce, such error messages result in immediate lack of confidence in the website and organizations risk losing confidence and business from the majority of consumers, that's normal as your certificate isn't signed yet by CA such as Thawte or Verisign who will verify the identity of the requester and issue a signed certificate.
    Java KeyStore Tutorial_html_15195a43
  6. You can click Proceed anyway till you receive you signed certificate.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....

 

14 COMMENTS

  1. Very interesting article – would it be possible to share an image which explains / shows a mutual authentication? In other words, steps when a server has to validate clients as well.
    Thanks in advance.
  2. Nice article! I would like to indicate the tool kse (keyStore explorer) at sourceforge. Not affiliate in any way but it sure makes it easier to understand the concepts since the GUI hides the command line, intimidating at first for some.
    Nothing wrong with command line , I use myself everyday and should be the preferred way in a production server, but a GUI helps while one is not familiar with the concepts.
  3. Also, I forgot:
    Whats up with that spy at the browser window? is some theme, plugin, or something, hummm…, else ? :)
  4. It’s a good point, it adds more security stack. Just, take a look on this image which explains the mutual authentication.
  5. Very nice article. Also thanks for sharing the diagram on mutual authentication. I have a question on enryption though. specifically this point:
    ==
    The browser then uses the public key, to encrypt a random symmetric encryption key and sends it to the server with the encrypted URL required as well as other encrypted http data
    ==
    If the URL is also encrypted, how will domain name resolution happen as the DNS server can not decrypt the URL since only the private key can do the same and that is available only with the origin server.
    -Prasad
    • Thanks for raising this point. Although the https guarantees the encrypted secure connection where the whole URL with its data are encrypted, DNS lookup request which resolves the domain name like “javacodegeeks.com” to numbers like “64.64.30.146” is sent through a separate unencrypted traffic before encrypting the URL, leaving you open to spoofing and man-in-the-middle attacks. DNSCrypt can lock that down.
      • Thanks for your response. On DNSCrypt, does it come integrated with browsers or is it an add-on? How can it be plugged in when a user wants to access a secure site?
        -Prasad
  6. I also wanted to know which ciphers and hashing are used in the SSL communication.
  7. for relative urls on an html page [ones of the type “../images/someImage.jpg”], will the browser fetch them on a secure channel? was curious to know how it is accomplished.
  8. Hi,
    I am created my own keystone using RSA algorithm,
    I am encryption and decryption small data is fine, but I try to encrypt long data(my data having a 5725 characters), I am getting “java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block” exception in java. please help me how to solve this problem.
  9. Hi Ashraf,
    I am trying to create sso login using cas for some of the applications in Tomcat.
    I have built cas and have deployed the cas on Tomcat 7.0
    Can you please tell me about the configurations to be made for the applications in Tomcat to get SSO Login through cas.
    Regards,
    Chethan SK