• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 591
  • Last Modified:

How to create JAR file and sign it

I have compiled .java file with netbeans and i have .class file now.I wonder how can i add it to JAR archive and sign it with keytool?.I have verisign ceritificate ,key pair and store pass.
0
Maranellas
Asked:
Maranellas
  • 11
  • 8
  • 5
1 Solution
 
CEHJCommented:
You should be able to add the Ant jarsigner task to your build file (manually if necessary)
0
 
MaranellasAuthor Commented:
Well when i select Packing option it still doesn't build JAR file just compiles the java file to class
0
 
objectsCommented:
the jar should be in the dist directory

following discusses how to get it signed

http://stoken-tips-and-tricks.blogspot.com/2007/03/howto-use-netbeans-and-ant-to-auto-run.html

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Below are what changes i made to my NB build file (there are two [can't remember which one]) to do this and it worked for me:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="SiteWatch" default="default" basedir=".">
    <description>Builds, tests, and runs the project SiteWatch.</description>
    <!--
 
    There exist several targets which are by default empty and which can be 
    used for execution of your tasks. These targets are usually executed 
    before and after some main targets. They are: 
 
      -pre-init:                 called before initialization of project properties
      -post-init:                called after initialization of project properties
      -pre-compile:              called before javac compilation
      -post-compile:             called after javac compilation
      -pre-compile-single:       called before javac compilation of single file
      -post-compile-single:      called after javac compilation of single file
      -pre-compile-test:         called before javac compilation of JUnit tests
      -post-compile-test:        called after javac compilation of JUnit tests
      -pre-compile-test-single:  called before javac compilation of single JUnit test
      -post-compile-test-single: called after javac compilation of single JUunit test
      -pre-jar:                  called before JAR building
      -post-jar:                 called after JAR building
      -post-clean:               called after cleaning build products
 
    (Targets beginning with '-' are not intended to be called on their own.)
 
    Example of inserting an obfuscator after compilation could look like this:
 
        <target name="-post-compile">
            <obfuscate>
                <fileset dir="${build.classes.dir}"/>
            </obfuscate>
        </target>
 
    For list of available properties check the imported 
    nbproject/build-impl.xml file. 
 
 
    Another way to customize the build is by overriding existing main targets.
    The targets of interest are: 
 
      -init-macrodef-javac:     defines macro for javac compilation
      -init-macrodef-junit:     defines macro for junit execution
      -init-macrodef-debug:     defines macro for class debugging
      -init-macrodef-java:      defines macro for class execution
      -do-jar-with-manifest:    JAR building (if you are using a manifest)
      -do-jar-without-manifest: JAR building (if you are not using a manifest)
      run:                      execution of project 
      -javadoc-build:           Javadoc generation
      test-report:              JUnit report generation
 
    An example of overriding the target for project execution could look like this:
 
        <target name="run" depends="SiteWatch-impl.jar">
            <exec dir="bin" executable="launcher.exe">
                <arg file="${dist.jar}"/>
            </exec>
        </target>
 
    Notice that the overridden target depends on the jar target and not only on 
    the compile target as the regular run target does. Again, for a list of available 
    properties which you can use, check the target you are overriding in the
    nbproject/build-impl.xml file. 
 
    -->
    <!-- Do we *really* have to use this kludge to stop deletion of the config
    file during a clean? -->
    <target name="-post-init">
        <copy file="backup/log4j.xml" todir="build/classes" />
    </target>
    
    <target name="sign">
        <signjar description="Sign the jars for JWS - IDE's seems to be broken"
                 keystore="${user.home}/.keystore"
                 storepass="xxxxxxxxx"
                 alias="business">
            <path>
                <fileset dir="." includes="**/*.jar" />
            </path>                     
        </signjar>
    </target>
</project>

Open in new window

0
 
MaranellasAuthor Commented:
Where exectly the dist directory located?

because i cannot find it in "C:\Users\Gio-client\Documents\NetBeansProjects\Client\build\classes"
0
 
objectsCommented:
should be in your build directory (do a file search)

0
 
CEHJCommented:
The location of the jar/dist directory isn't relevant at this point
0
 
MaranellasAuthor Commented:
C:\Users\Gio-client\Documents\NetBeansProjects\Client\build.xml:75: Warning: Could not find file C:\Users\Gio-client\Documents\NetBeansProjects\Client\backup\log4j.xml to copy.
BUILD FAILED (total time: 0 seconds)
0
 
CEHJCommented:
You need to take out my copy task to copy the log4j config file if you're not using logging (you should be ;-))
0
 
objectsCommented:
careful replacing your build file, you could break things. Best keep changes to thzt a minimum. Just add the new sign task as described above to what you already have.

0
 
MaranellasAuthor Commented:
okay here we go

init:
deps-jar:
C:\Users\Gio-client\Documents\NetBeansProjects\Client\nbproject\build-impl.xml:328: Directory C:\Users\Gio-client\Documents\NetBeansProjects\Client\build\classes creation was not successful for an unknown reason
BUILD FAILED (total time: 0 seconds)


it set read only permissions on the folder and i'm unable to delete it manualy either


<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="Client" default="default" basedir=".">
    <description>Builds, tests, and runs the project Client.</description>
    <import file="nbproject/build-impl.xml"/>
    <!--
 
    There exist several targets which are by default empty and which can be 
    used for execution of your tasks. These targets are usually executed 
    before and after some main targets. They are: 
 
      -pre-init:                 called before initialization of project properties
      -post-init:                called after initialization of project properties
      -pre-compile:              called before javac compilation
      -post-compile:             called after javac compilation
      -pre-compile-single:       called before javac compilation of single file
      -post-compile-single:      called after javac compilation of single file
      -pre-compile-test:         called before javac compilation of JUnit tests
      -post-compile-test:        called after javac compilation of JUnit tests
      -pre-compile-test-single:  called before javac compilation of single JUnit test
      -post-compile-test-single: called after javac compilation of single JUunit test
      -pre-jar:                  called before JAR building
      -post-jar:                 called after JAR building
      -post-clean:               called after cleaning build products
 
    (Targets beginning with '-' are not intended to be called on their own.)
 
    Example of inserting an obfuscator after compilation could look like this:
 
        <target name="-post-compile">
            <obfuscate>
                <fileset dir="${build.classes.dir}"/>
            </obfuscate>
        </target>
 
    For list of available properties check the imported 
    nbproject/build-impl.xml file. 
 
 
    Another way to customize the build is by overriding existing main targets.
    The targets of interest are: 
 
      -init-macrodef-javac:     defines macro for javac compilation
      -init-macrodef-junit:     defines macro for junit execution
      -init-macrodef-debug:     defines macro for class debugging
      -init-macrodef-java:      defines macro for class execution
      -do-jar-with-manifest:    JAR building (if you are using a manifest)
      -do-jar-without-manifest: JAR building (if you are not using a manifest)
      run:                      execution of project 
      -javadoc-build:           Javadoc generation
      test-report:              JUnit report generation
 
    An example of overriding the target for project execution could look like this:
 
        <target name="run" depends="Client-impl.jar">
            <exec dir="bin" executable="launcher.exe">
                <arg file="${dist.jar}"/>
            </exec>
        </target>
 
    Notice that the overridden target depends on the jar target and not only on 
    the compile target as the regular run target does. Again, for a list of available 
    properties which you can use, check the target you are overriding in the
    nbproject/build-impl.xml file. 
 
    -->
	    <target name="-post-init">
    </target>
    
    <target name="sign">
        <signjar description="Sign the jars for JWS - IDE's seems to be broken"
                 keystore="${user.home}/.keystore"
                 storepass="welcome1"
                 alias="mykey">
            <path>
                <fileset dir="." includes="**/*.jar" />
            </path>                     
        </signjar>
    </target>
</project>

Open in new window

0
 
CEHJCommented:
Get rid of

>>
           <target name="-post-init">
    </target>
>>

entirely
0
 
MaranellasAuthor Commented:
Well now it created jar file in dist folder,but it's not signed with valid certificate as it should be.
0
 
CEHJCommented:
Well i set it up to need explicit activation, but if you want it automatic, you could try changing that

>>
           <target name="-post-init">
    </target>
>>

you just got rid of to


<target name="-post-jar">
    <antcall target="sign">
</target>

Open in new window

0
 
CEHJCommented:
Oops


<target name="-post-jar">
    <antcall target="sign"/>
</target>

Open in new window

0
 
objectsCommented:
see the link I posted earlier, it show you need to call the sign task from *inside* -post-jar
0
 
MaranellasAuthor Commented:
Well it got signed now,but using .keystore .I have versign.cer file how can i sign with it?
0
 
CEHJCommented:
0
 
objectsCommented:
0
 
MaranellasAuthor Commented:
i get the following error

C:\Program Files\Java\jdk1.6.0_13\bin>keytool -import -trustcacerts -keystore .k
eystore -alias mykey -file verisign.cer
Enter keystore password:
keytool error: java.security.cert.CertificateParsingException: invalid DER-encod
ed certificate data

C:\Program Files\Java\jdk1.6.0_13\bin>
0
 
CEHJCommented:
Make sure you have the right kind of cert
0
 
MaranellasAuthor Commented:
All done thanks for your time
0
 
CEHJCommented:
:-)

No problem
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 11
  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now