Wednesday, August 17, 2011
Mime type of file in Java
File f = new File("sample.gif");
System.out.println("Mime Type of " + f.getName() + " is " +
new MimetypesFileTypeMap().getContentType(f));
More Reference :
http://www.rgagnon.com/javadetails/java-0487.html
OutOfMemoryError in Eclipse
Window -> Preferences -> Java -> InstalledJRE -> Edit -> Default VM arguments
-Xms128M -Xmx1024M
Thursday, October 1, 2009
Count number of specific char in a string in Java
String s = "Count, the number,, of commas.";
int numberOfCommas = s.replaceAll("[^,]","").length();
Wednesday, July 15, 2009
Keytool Commands
- Create an empty keystore
- keytool -genkey -alias foo -keystore truststore.jks
keytool -delete -alias foo -keystore truststore.jks - Generate a private key and an initial certificate as a JKS keystore
- keytool -genkey -keyalg RSA -alias "selfsigned" -keystore KEYSTORE.jks -storepass "secret" -validity 360
- you can also pass the data for the DN of the certificate as command-line parameters:
-dname "CN=${pki-cn}, OU=${pki-ou}, O=${pki-o}, L=${pki-l}, S=${pki-s}, C=${pki-c}" - Generate a secret key that can be used for symmetric encryption. For this to work, you need to make use of a JCEKS keystore.
- keytool -genseckey -alias "secret_key" -keystore KEYSTORE.jks -storepass "secret" -storetype "JCEKS"
- Generate a Certificate Signing Request for a key in a JKS keystore
- keytool -certreq -v -alias "selfsigned" -keystore KEYSTORE.jks -storepass "secret" -file MYCSR.csr
- Import a (signed) certificate into a JKS keystore
- keytool -import -keystore KEYSTORE.jks -storepass "secret" -file MYCERT.crt
- add a public certificate to a JKS keystore, eg the JVM truststore
- keytool -import -trustcacerts -alias "sensible-name-for-ca" -file CAcert.crt -keystore MYSTORE.jks
- If the JVM truststore contains your certificate or the certificate of the root CA that signed your certificate, then the JVM will trust and thus might accept your certificate. The default truststore already contains the root certificates of most commonly used sommercial CA's.Use this command to add another certificate for trust:
- keytool -import -trustcacerts -alias "sensible-name-for-ca" -file CAcert.crt -keystore $JAVA_HOME/lib/security/cacerts
- the default password of the Java truststore is "changeit".
if $JAVA_HOME is set to the root of the JDK, then the truststore is it $JAVA_HOME/jre/lib/security/cacerts
keytool does NOT support adding trust certificates to a PKCS12 keystore (which is very unfortunate but probably a good move to promote JKS) - delete a public certificate from a JAVA keystore (JKS; eg JVM truststore)
- keytool -delete -alias "sensible-name-for-ca" -keystore $JAVA_HOME/lib/security/cacerts
- the default password of the Java truststore is "changeit".
if $JAVA_HOME is set to the root of the JDK, then the truststore is it $JAVA_HOME/jre/lib/security/cacerts - List the certificates inside a keystore
- keytool -list -v -keystore KEYSTORE.jks
-
-storetype pkcs12can be used - Get information about a stand-alone certificate
- keytool -printcert -v -file MYCERT.crt
- Convert a JKS file to PKCS12 format (Java 1.6.x and above)
- keytool -importkeystore -srckeystore KEYSTORE.jks -destkeystore KEYSTORE.p12 -srcstoretype JKS -deststoretype PKCS12 -srcstorepass mysecret -deststorepass mysecret -srcalias myalias -destalias myalias -srckeypass mykeypass -destkeypass mykeypass -noprompt
keytool
keytool does not support management of private keys inside a keystore. You need to use another tool for that. If you are using the JKS format, that means you need another java-based tool. extkeytool from the Shibboleth distribution can do this. Signing the MIDlet
A commercial application which requires access to some specific APIs like JSR 75, JSR 205, JSR 135 etc. benefits from getting signed with a commercial certificate from third party certificate providers like VeriSign or Thawte. Before buying any certificate from these third parties, you have to make sure that the target phone's of your device has the corresponding root certificate installed and set to allow MIDlet signing. You can check this from the settings of the target mobile device.
The following paragraphs contain the description of the process and steps required to get a certificate from VeriSign and to sign & test you MIDlet.
Getting the certificate
Step 1
Make sure, you have the latest JDK installed on your computer.
Step 2
Create a keystore
You have to generate a public/private key pair, for this enter the following command, specifying a name for your keystore and an alias as well.
C:\jdk1.3\bin\keytool -genkey -keyalg rsa -keystore-alias
Keytool prompts you to enter a password for your keystore, your name, organization, and address. The public/private key pair generated by keytool is saved to your keystore and will be used to sign J2ME applications. This key is never sent to VeriSign and is required to sign code. So you should make a copy of the public/private key pair and store it in a safe deposit box or other secure location. If the key is lost or stolen, contact VeriSign immediately to have it revoked.
Step 3
Create a CSR
You need to generate a Certificate Signing Request (CSR) for the enrollment process.
1. The following command requests Keytool to create a CSR for the key pair in the keystore:
C:\jdk1.3\bin\keytool -certreq -file certreq.csr -keystore2. Begin the enrollment process for a Code Signing ID from the products and services section of the VeriSign Web site.
3. Copy the contents of the CSR and paste them directly into the VeriSign enrollment form. Open the file in a text editor that does not add extra characters (Notepad or Vi are recommended).
This is the end of requesting the certificates from VeriSign.
Signing the MIDlet
Step 1
After VeriSign has verified your request, they will send an email with the digital id attached. You have to import it to the keystore.
To import your Sun Java Signing Code Signing Digital ID into your keystore, enter the following code with the path correct name for your file (for example, "cert.cer") to your Code Signing Digital ID.
C:\jdk1.3\bin\keytool -import -trustcacerts -keystore-alias -file cert.cer
Step 2
Now sign the application by the two command prompt
java -jar JadTool.jar [ -addcert -alias keyAlias [ -keystore keystore ] [ -storepass password ] -inputjad inputJadFile -outputjad outputJadFile ]java -jar JadTool.jar [ -addjarsig [ -jarfile jarFile ] -alias keyAlias [ -keystore keystore ] -storepass password -keypass keyPassword -inputjad inputJadFile -outputjad outputJadFile ]
Wednesday, July 1, 2009
History of JAVA
James Gosling initiated the Java language project in June 1991 for use in one of his many set-top box projects.[5] The language, initially called Oak after an oak tree that stood outside Gosling's office, also went by the name Green and ended up later renamed as Java, from a list of random words.[6] Gosling aimed to implement a virtual machine and a language that had a familiar C/C++ style of notation.[7]
Sun released the first public implementation as Java 1.0 in 1995. It promised "Write Once, Run Anywhere" (WORA), providing no-cost run-times on popular platforms. Fairly secure and featuring configurable security, it allowed network- and file-access restrictions. Major web browsers soon incorporated the ability to run Java applets within web pages, and Java quickly became popular. With the advent of Java 2 (released initially as J2SE 1.2 in December 1998), new versions had multiple configurations built for different types of platforms. For example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for mobile applications. J2SE designated the Standard Edition. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.
In 1997, Sun Microsystems approached the ISO/IEC JTC1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process.[8] Java remains a de facto standard, controlled through the Java Community Process.[9] At one time, Sun made most of its Java implementations available without charge, despite their proprietary software status. Sun generated revenue from Java through the selling of licenses for specialized products such as the Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and Runtime Environment (JRE) (a subset of the SDK); the primary distinction involves the JRE's lack of the compiler, utility programs, and header files.
On 13 November 2006, Sun released much of Java as free and open source software under the terms of the GNU General Public License (GPL). On 8 May 2007 Sun finished the process, making all of Java's core code available under free software / open-source distribution terms, aside from a small portion of code to which Sun did not hold the copyright.[10]