How to create inputstream to a .jar

i want to make an inputstream that points to the jar file
my main class was executed from.
this dosent seem to work:
InputStream is = CypherIO.class.getResourceAsStream("jar.jar");i guess a "resource" is something inside a jar...
also it would be nice if i could rename the jar and it would still work.
my goal is to rewrite the entire jar.

This would try to find a file called "jar.jar" in your
classpath, which probably means INSIDE your program's
jar file. There is no way to find the location of the
jar file your classes are in. The only thing you could
do is to get the classpath system property and walk
through it (parsing it), looking for your program's
jar file. But rewriting the jar file while your
program is running could wreak some serious havoc with
your poor little JVM.well, i could get the directory structure form a url created from one of the classes inside the jar.
but even with the directory i dont know how i would create the inputstream.
then i guess well find out what happens when i try and rewrite it. it seems likely that the vm would be done with the jar once it gets the code it needs.

Similar Messages

  • How to create and use library JAR files with command-line tools?

    Development Tools -> General Questions:
    I am trying to figure out how to put utility classes into JAR files and then compile and run applications against those JAR files using the command-line javac, jar, and java tools. I am using jdk1.7.0_17 on Debian GNU/Linux 6.0.7.
    I have posted a simple example with one utility class, one console application class, and a Makefile:
    http://holgerdanske.com/users/dpchrist/java/examples/jar-20130520-2134.tar.gz
    Here is a console session:
    2013-05-20 21:39:01 dpchrist@desktop ~/sandbox/java/jar
    $ cat src/com/example/util/Hello.java
    package com.example.util;
    public class Hello {
        public static void hello(String arg) {
         System.out.println("hello, " + arg);
    2013-05-20 21:39:12 dpchrist@desktop ~/sandbox/java/jar
    $ cat src/com/example/hello/HelloConsole.java
    package com.example.hello;
    import static com.example.util.Hello.hello;
    public class HelloConsole {
        public static void main(String [] args) {
         hello("world!");
    2013-05-20 21:39:21 dpchrist@desktop ~/sandbox/java/jar
    $ make
    rm -f hello
    find . -name '*.class' -delete
    javac src/com/example/util/Hello.java
    javac -cp src src/com/example/hello/HelloConsole.java
    echo "java -cp src com.example.hello.HelloConsole" > hello
    chmod +x hello
    2013-05-20 21:39:28 dpchrist@desktop ~/sandbox/java/jar
    $ ./hello
    hello, world!I believe I am looking for:
    1. Command-line invocation of "jar" to put the utility class bytecode file (Hello.class) into a JAR?
    2. Command-line invocation of "javac" to compile the application (HelloConsole.java) against the JAR file?
    3. Command-line invocation of "java" to run the application (HelloConsole.class) against the JAR file?
    I already know how t compile the utility class file.
    Any suggestions?
    TIA,
    David

    I finally figured it out:
    1. All name spaces must match -- identifiers, packages, file system, JAR contents, etc..
    2. Tools must be invoked from specific working directories with specific option arguments, all according to the project name space.
    My key discovery was that if the code says
    import com.example.util.Hello;then the JAR must contain
    com/example/util/Hello.classand I must invoke the compiler and interpreter with an -classpath argument that is the full path to the JAR file
    -classpath ext/com/example/util.jarThe code is here:
    http://holgerdanske.com/users/dpchrist/java/examples/jar-20130525-1301.tar.gz
    Here is a console session that demonstrates building and running the code two ways:
    1. Compiling the utility class into bytecode, compiling the application class against the utility bytecode, and running the application bytecode against the utility bytecode.
    2. Putting the (previously compiled) utility bytecode into a JAR and running the application bytecode against the JAR. (Note that recompiling the application against the JAR was unnecessary.)
    (If you don't know Make, understand that the working directory is reset to the initial working directory prior to each and every command issued by Make):
    2013-05-25 14:02:47 dpchrist@desktop ~/sandbox/java/jar
    $ cat apps/com/example/hello/Console.java
    package com.example.hello;
    import com.example.util.Hello;
    public class Console {
        public static void main(String [] args) {
         Hello.hello("world!");
    2013-05-25 14:02:55 dpchrist@desktop ~/sandbox/java/jar
    $ cat libs/com/example/util/Hello.java
    package com.example.util;
    public class Hello {
        public static void hello(String arg) {
         System.out.println("hello, " + arg);
    2013-05-25 14:03:03 dpchrist@desktop ~/sandbox/java/jar
    $ make
    rm -rf bin ext obj
    mkdir obj
    cd libs; javac -d ../obj com/example/util/Hello.java
    mkdir bin
    cd apps; javac -d ../bin -cp ../obj com/example/hello/Console.java
    cd bin; java -cp .:../obj com.example.hello.Console
    hello, world!
    mkdir -p ext/com/example
    cd obj; jar cvf ../ext/com/example/util.jar com/example/util/Hello.class
    added manifest
    adding: com/example/util/Hello.class(in = 566) (out= 357)(deflated 36%)
    cd bin; java -cp .:../ext/com/example/util.jar com.example.hello.Console
    hello, world!
    2013-05-25 14:03:11 dpchrist@desktop ~/sandbox/java/jar
    $ tree -I CVS .
    |-- Makefile
    |-- apps
    |   `-- com
    |       `-- example
    |           `-- hello
    |               `-- Console.java
    |-- bin
    |   `-- com
    |       `-- example
    |           `-- hello
    |               `-- Console.class
    |-- ext
    |   `-- com
    |       `-- example
    |           `-- util.jar
    |-- libs
    |   `-- com
    |       `-- example
    |           `-- util
    |               `-- Hello.java
    `-- obj
        `-- com
            `-- example
                `-- util
                    `-- Hello.class
    19 directories, 6 filesHTH,
    David

  • How to create java archive using jar command

    hello
    what is the syntax for creating java acrchive using jar command line
    jar -c ???????
    urgent plzz

    jar cf myjar.jar classfiles...
    If you have a manifest file you want to add:
    jar cfm myjar.jar manifest.mf classfiles...
    Also check the documentation:
    http://java.sun.com/j2se/1.3/docs/tooldocs/tools.html
    You can find a link to jar there.

  • How to create a deployable ejb(.jar file) when we use Oracle application sever?

    Hi,
    In case if we use Weblogic we give following command on console to create a deployable ejb, while weblogic.jar is in classpath.
    java weblogic.ejbc temp.jar StudentBean.jar
    1)What are the classes or jar files we have to set in classpath,
    2)What console based command(like what I mentioned above for weblogic) can we use,
    so that we can create a deployable jar file in case if we are using Oracle application server.
    thanks,
    Milind

    You can discover the origins of a loaded class programatically. You want to print:
    TheClass.class.getProtectionDomain().getCodeSource().getLocation().toString();

  • How to create dll file and jar files in oracle forms ? and how to use in oracle forms?

    pls teach me....

    Your question is more related to batch programming.
    From what I understand, you want to add 100 [ :o ] jars to the classpath to execution and you probably don't want to write all the names in the batch file. There's a simple way of adding jars by running a loop. The code should look like the following:
    dir /b "%LIBDIR%\*.jar" > temp.tmp
    FOR /F %%I IN (temp.tmp) DO SET CLASSPATH=%CLASSPATH%;"%LIB_DIR%\%%I"
    del temp.tmp

  • How to create package and import from jar file?

    Hi all,
    I am writing a software and I am not sure how to create a package for the classes.
    Say I have two classes in the same directory as follows:
    testA.java
    ==========
    package AB;
    public class testA
    public static void main(String[] args){
         testB myB = new testB();
         System.out.println("A test");
    testB.java
    ===========
    package AB;
    public class testB
    public testB(){
         System.out.println("B constructor");
    both file compile without the package heading;
    both file compile using: javac -classpath .\ *.java
    Question 1:
    I cannot run testA by: java -classpath .\ testA
    I think it is a syntax error. What is the correct one?
    If I run testA by: java testA
    The only output I get is: A test
    But I am expecting: B constructor /n A test
    What went wrong?
    Question 2:
    I need to use APIs of another software. I have downloaded a .jar file (xxx.jar) with all the classes in it. And I have put "import xxx.*;" in my source file. But the compiler complains about the importing. What is the right way to copmile it?
    I have read a couple of tutorials but they don't answer my question.
    (I am using windows2000 and don't have the classpath variable.)
    Hope some one can help.
    Thanks a lot

    Try moving testA out of the package and importing 'AB.*;'
    If you have:
    ./testA.class
    ./AB/testb.class
    Then to execute testA from ./ type: java -cp . testA

  • How to create .JAR file in JSE 8?

    I don't know how to create a .JAR file from my project. I found a .JAR file in .../dist under my project folder, but I could not run it.

    This Swing app has 2 class files, requires Swing Library to be imported (of course), and requires no other files. It seems like the program does not import Swing Lib. so that it can display the JFrame inside.
    1. I have JRE installed separately so I can run Jar files directly by double-clicking. I also tried to run within command prompt, and had the same result. No error or exception warning found.
    2. As I said, the program requires no other files.
    3. I repeat that it works fine within JSE as well as JBuilder. the Jar created by JBuilder (same code) works fine, too.
    4. This Jar file is the only one Jar that I found in my project folder, and has the same name with the project. (But anyway, there's no folder contains 2 files with the same name, so this verification is not necessary).

  • How to create a .jar file in wlcs3.1

    hello friends,
    hi friends, im working on weblogic commerce server3.1 . i dont know how to create a .jar file in weblogic commerce server3.1. can any one knows send email to me.
    thanks.
    hari

    $JDK_HOME/bin/jar (%JDK_HOME%\bin\jar) shows you the syntax
    where JDK_HOME is your jdk installation directory.
    Kumar
    hari wrote:
    hello friends,
    hi friends, im working on weblogic commerce server3.1 . i dont know how to create a .jar file in weblogic commerce server3.1. can any one knows send email to me.
    thanks.
    hari

  • How to create executable jar in eclipse using linking in compiler

    Hi,
    I have a project in Eclipse. I wrote already an ant build file for creating an executable jar which includes all project files.
    Now I want to create a second executable jar which is a subset of the project (a limited number of classes are necessary).
    I have a main class and I was looking for a way to generate a jar file which includes the main class and all the depending classes.
    Is there a way to do it? With ant or with another tool?
    Is it possible to compile a project given a main class, and include all depending classes?
    Regards,
    Biebel
    Edited by: biebel1975 on Jan 7, 2009 10:21 AM

    biebel1975 wrote:
    Some more information:
    When I compile all the classes, the jar file is +/- 3.5Mb inclusive the server side logic.
    The subproject I want to build, should be less then 1Mb (Only some screens and beans, no business logic ((only +/- 10% of the projects code) )).
    My question is: How can I create a executable jar with only the necessary class files which depends on the main class. (Linked with the imports).
    Is there a tool to do that? Can it be done with an ANT script? If so, how does the script looks?
    What are you talking about?
    You already said that you are using Ant.
    Ant uses script files.
    Since you have one with X items in it and you want something new with X-Y items in it the obviously it would make sense to construct an ant script that does less than the other ant script.
    The following provides a huge amount of information about Ant.
    [http://ant.apache.org/resources.html]
    Other than that no one can tell you want your Ant script will look like except that it will do less than the one that you already have.

  • How to create a JAR

    Hi
    I'm very new to Java. I have a private project to do a little admin application for bulk changes to my itunes library via their COM API. Previously I used several javascripts to do this so I don't expect a lot of work to adapt them to Java. I use eclipse and have downloaded com4j (https://com4j.dev.java.net/servlets/ProjectDocumentList) to create wrappers to the COM-objects in iTunes. the quick intro to com4j say that the wrappers are created by the tlbimp.jar. I know that JAR is a a java archive created by jdk using jar.exe but I can't get out a tlbimp.jar file how much i try. If I do something like this
    jar -cvf tlbimp.jar *
    Does that make sense? What prerequisites are important? Does jar.exe have to be in som path variable in windows? Should I be in the folder where the classes are or one level up? Anything else
    Edited by: 802710 on 2010-okt-15 03:16

    802710 wrote:
    Hi
    I'm very new to Java. I have a private project to do a little admin application for bulk changes to my itunes library via their COM API. Previously I used several javascripts to do this so I don't expect a lot of work to adapt them to Java. I use eclipse and have downloaded com4j (https://com4j.dev.java.net/servlets/ProjectDocumentList) to create wrappers to the COM-objects in iTunes. the quick intro to com4j say that the wrappers are created by the tlbimp.jar. I know that JAR is a a java archive created by jdk using jar.exe but I can't get out a tlbimp.jar file how much i try. If I do something like this
    jar -cvf tlbimp.jar *
    Does that make sense? What prerequisites are important? Does jar.exe have to be in som path variable in windows? Should I be in the folder where the classes are or one level up? Anything elseIt sounds to me like the documentation is saying that Java classes in the tlbimp.jar are used to create the wrappers - you do not need to create that jar. I bet if you look at the zip file you downloaded, you will find tlbimp.jar inside the zip file. You need to extract that jar and any other jars inside the zip file and configure Eclipse to use them.

  • How to create the file   .jar in  openScript .

    How to create the file .jar in openScript .
    thanks

    Ahh ok.
    What I usually do is create a script called My Functions then I add new functions by right clicking in OpenScript's tree view
    Add-> Other -> General -> New Function.
    Once you have created the functions you can either place the script on a shared repository or export it manually an then share it File -> export
    Then from the script that you want to call the functions go to:
    Script -> Script Properties -> Script Assets -> Script -> Add then select the My Functions script once that is done you can call the methods from the script by:
    Add -> Script Functions -> Script My Functions then select the function you wish to use.
    Hope this helps
    Alex

  • How to run .jar on linux & how to create .jar file using java?

    hi, may i know how to run .jar on linux & how to create .jar file using java? Can u provide the steps on doing it.
    thanks in advance.

    Look at the manual page for jar:
    # man jar
    Also you can run them by doing:
    # java -jar Prog.jar

  • How to create executable jar files??

    how to create executable jar files??
    thx :)

    Using JAR Files to Deploy
    http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/jar.html#jar

  • How to create/read/write textfiles in a jar archive?

    Hi!
    I would like to know how to create, read and write text file in a jar file?
    //Marcus

    You want to update "files" that are inside a jar archive? You can't. At least not very easily; to modify a file you need to create a new version of the jar, copy over everything else, and add the new version of that file. Look for a different design that doesn't require updating a jar.
    PC²

  • How to create jar file

    Hi
    I installed the j2me software j2me_wireless_toolkit-2_2-windows.exe. After installing the path is C:\WTK22\
    when iam creating the new project. Its woking fine. I can able to see the demo on the simulator. After building the porject. inside the bin folder there is only JAD file and MF files are there. There is no jar file for the project. If i want to execute the project in my mobile i need the jar file.
    So help me how to create the executable jar file for the project in the software.
    Regards
    A.Krishnaraj

    How to create package after building the project. Is there is any link are presented in software or we have to do manually.
    Regards
    A.Krishnaraj

Maybe you are looking for