Random Noise

August 14, 2007

Signing a JAR file with .spc and .pvk file

Filed under: Java — Tags: , , , — Vivek Unune @ 2:58 am

0. You must have a .spc(Certificate) file and a .pvk(Key) file

1. Download pvkimprt.exe from here.

And yes don’t forget to install the downloaded exe!!

2. To generate a .pfx file run:

pvkimprt -PFX myspcfile.spc mypvkfile.pvk

type in the password and give a pathname

3. Import this file from Firefox

4. Export the certificate from Firefox to a .p12 file

5. This file can be used with jarsigner

6. We also need to know the alias of the .p12 file, so copy the .p12 file to the Java bin directory and run:

keytool -list -storetype pkcs12 -keystore mycert-p12.p12

7. Enter the password

8. Then you will see output like this:

Keystore type: pkcs12

Keystore provider: SunJSSE

Your keystore contains 1 entry

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx, Aug 13, 2007, keyEntry,

Certificate fingerprint (MD5):

hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh

9. The xxxx-xxx… number is the alias for the key

10. To sign a jar run:
jarsigner -storetype pkcs12 -keystore mycert-p12.p12 myjar.jar \
“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”

Update 18th April 2008:

You can also sign the jar using pkcs12 or .p12 certificate using this:

jarsigner -keystore mycert.p12 -storepass mystorepass -keypass mykeypass -storetype “pkcs12″ -signedjar myjar-signed.jar myjar.jar “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”

To verify your signed jar do:

jarsigner -verify -verbose -keystore mycert.p12 -storepass mystorepass -keypass mykeypass -storetype “pkcs12″ myjar-signed.jar

June 30, 2007

Installing ANT

Filed under: Java, Windows Tip — Tags: , , — Vivek Unune @ 2:41 pm
  1. Download the latest zip from http://ant.apache.org/bindownload.cgi.
  2. Extract it to a directory. For example C:\Users\Joe\apache-ant-1.7-bin.
  3. Edit your environment variables: On Windows XP: Right click on
    My Computer and select properties, then select Advance tab. On
    Vista: Right click on Computer and select properties => Advanced
    System Settings, then click on Environment Variables button.
  4. Add two system environment variables:
    JAVA_HOME = C:\Program Files\Java\jdk1.6.0_01
    ANT_HOME = C:\Users\Joe\apache-ant-1.7-bin
  5. Edit PATH envroment variable and add the following to the end:
    ;%JAVA_HOME%\bin;%ANT_HOME%\bin;

May 18, 2007

JNI debugging using Eclipse and Visual Studio 2005

Filed under: Java, VC++, linux — Tags: , , , , — Vivek Unune @ 11:58 pm

Recently I was involved in a project that involved java code accessing native c++ libraries. All went fine from creation of the JNI functions to its implementation. In one of the functions has java.nio.ByteBuffer as parameter. On the java side this buffer was initialized using allocateDirect. On the c++ side, the buffer content is altered and size gets reduced. The limit is changed using limit(int). I managed this all using cout/printfs/err to print the debug information.

I put up a screen cast (without voice) for anyone who needs to watch this method inaction. Download this video from here: http://www.hotlinkfiles.com/files/1266155_p61kq/jni-debug.divx

Short description of the same:

So, I really needed to look at the live data. Hence needed a way to debug JNI c++ code. So, as everyone else I turned to Google. Finally this article described the need for dual debuggers. I understood that we need to start the java application that uses jvm in debug mode (-Xdebug). We also need to make sure that Eclipse and Visual Studio co-operate. So we need the -Xrunjdwp jvm argument. This will specify the protocol and port address where Eclipse (remote java application) will listen to.

So, open the JNI C++ VS 2005 project and navigate to project->settings->debugging. in command specify the path to java.exe, in command arguments add this:

-Xmx400m -Xms400m -Xdebug -Xnoagent \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 \
-Djava.library.path=”[path to debug folder containing JNI DLL]” \
-cp “.;[path to folder containing .class files for he java app]” \
[main java class]

And then set break points in the c++ code.

Then we need to create a remote java application configuration in Eclipse. Specify the port number (in this case 8000). Also in my case I had to add java source directory where Eclipse will search for the java code of the remote application. This is done from the source tab. Once done add break points where the java application loads the native library and where you call the native function.

Then start debugging the VS project (f5). The java application will be in suspended mode (because suspend=y argument) and wait for external debugger to attach. So, start the Eclipse’s remote java application configuration that we created in debug mode. And start debugging!

Blog at WordPress.com.