Results 1 to 3 of 3
  1. #1
    zeyarag is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default How to execute a class in a subdirectory in .jar file

    Dear All,

    I have a jar file "My.jar".
    In this "My.jar" file, I have a directory "build\classes".
    In this "build\classes" directory, I have a package "package1.sub1".
    In this "package1.sub1", I have class files "MyClassA.class", "MyClassB.class", etc. (package package1.sub1 is declared in all of these classes.)

    Now, I want to execute "package1.sub1.MyClassA.class" in "build\classes" directory of "My.jar" from the command line. How can I do that?

    I tried "java -cp My.jar build\classes\package1.sub1.MyClassA.class", but it was not successful. Please help me.

    Thank you very much.

    Best regards,

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    I'm not sure why you would package the build/classes folder into the jar file.?
    Usually you would create the jar file with the build/classes as the base directory, so that the first folder inside the jar file would be package1/ and then the command line "java -cp My.jar package1.sub1.MyClassA" (also without the .class extension) would just work.

    In theory it is technically possible to get the files inside build/classes inside the jar file to work, this can be done by creating a bootstrap piece of Java code that lives in the top level of the jar file that unpacks the jar file to a temporary location and then installs a URL class loader kind of class loader to refer to the build/classes/ where it now exists in this temporary folder to have it added onto the classpath. But unless there was a very compelling reason to do this, I would say just change how you build your jar file to have the build/classes be the jar root.

    For example, in Ant,
    Java Code:
      <target name="make-jar">
        <jar jarfile="My.jar"  basedir="build/classes" />

  3. #3
    zeyarag is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Thank you.

    Dear Travishein,

    Thank you very much for your kind help.

    Best regards,

Similar Threads

  1. How to execute a .class file from a Rightclick
    By Norm in forum Suggestions & Feedback
    Replies: 0
    Last Post: 09-09-2010, 09:58 PM
  2. jar file won't execute.
    By Splat in forum New To Java
    Replies: 5
    Last Post: 10-22-2009, 05:26 AM
  3. Replies: 0
    Last Post: 06-26-2008, 07:32 PM
  4. how to rename a subdirectory
    By richardzhang in forum New To Java
    Replies: 0
    Last Post: 08-10-2007, 04:28 AM
  5. Execute Jar file
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts