Get name of executing jar-file (who-am-i)

Hi,
I'm planning to use a jar-file for a JAVA-application that holds all classes and an additional file with initial values (init.txt). This should allow an easy way to change initial behavior without having to recompile the code (I mean I could put all the initial values directly into the code). So I simply change init.txt and deploy the jar-file.
To achieve this I need a way to find out the name of the jar-file that is currently executing. With that I can open the jar-file from within the code and do the rest (I'm not going to bother you with this ;) ).
In C it was pretty simple, an argv[0] would do it. But how does it work in JAVA? I only have access to the parameters passed to the program.
EXAMPLE:
========
The command line looks like this:
java -jar MyApp.jar
MyApp.jar contains this (among others):
|- MyApp.class
|- init.txt
In this example I need to get the information that I'm "MyApp.jar". I already have the rest of the code so I'm only missing this little thingy.
Thanks beforehands for help.

No, you should NOT need the name of your JAR file. You are probably coding it up the wrong way. If what you're after is to be able to read that "init.txt" file, then do so via something like:
InputStream initStream = getClass().getResourceAsStream("/init.txt");
rather than using filename-based API's.

Similar Messages

  • I am getting system tray icon when i execute jar file.

    Hi.,
    I am not getting system tray icon when i execute jar file. But a process is created and its running at background... When i execute the same program in net beans, i got system tray icon as well. why i didn't get system tray icon outside the net beans.? Can anyone help me on this..?????

    Karthik_Java wrote:
    Hi.,
    I am not getting system tray icon when i execute jar file. But a process is created and its running at background... When i execute the same program in net beans, i got system tray icon as well. why i didn't get system tray icon outside the net beans.? Can anyone help me on this..?????What version of the JRE is set as the default on your machine, as far as I'm aware the ability to crap up the notification area was only added in JSE6.

  • Error trying to run an executable Jar file

    Hi,
    I know a million people have asked this before. In the past 5 hours I have waded thru a dozen questions-answers-FAQs that were no help. So if anyone is willing to help a stupid person her is my problem:
    I developed a java program using the eclipse ide and it runs fine from within that ide.
    I packaged it into a Jar file, created a manifest file per the instructions.
    But when I double click on the jar file I get the following error from the Java Virtual Machine Loader;
    "Could not find main class. Program will exit."
    My Manifest file has the following text:
    Manifest-Version: 1.0
    Class-Path: swt.jar
    Main-Class: .graphicalUserInterface.AppStandAlone
    <blank line>
    the class AppStandAlone has the method "public static void main(String[] args) "
    it is in a package called "graphicalUserInterface".
    Any help anyone can give me would be greatly appreciated.

    Another 3 hours spent trying to run this java application in a Jar file.
    I identified that I did not have all class paths specified. So I tried to enter them in the "Class-Path:" line of the manifest file. But, according to the java tutorial, the syntax in a manifest file for classpaths is a space SEPARATING each path name. I have a space IN the path name and java is not understanding the paths correctly. For instance, I have a classpath c:\Progran Files\Java\etc... and java interprets this to be a classpath c:\Progran with Files\Java\etc as the program I want to run.
    I tried enclosing the paths in double quotes. Didn't work
    I tried enclosing the paths in single quotes. Didn't work
    I tried using a semicolon as a separator with double quotes. Didn't work
    I tried using a semicolon as a separator with single quotes. Didn't work
    I tried using a semicolon as a separator with no quotes. Didn't work
    I removed the "Class-Path:" line from the manifest and tried adding -cp param to the java command. Example:
    java -cp %classpath% -jar myapp.jar
    I tried all the variations above in the -cp param. None of them worked.
    The only thing that works is if I move executable jar file to the same directory as the required libraries. Then I specify a "Class-Path:" line with no spaces in the path names. Obviously this is not a permanent solution.
    If anyone else has the patience to help me I would really appreciate it.

  • [b]Simple Deploy problem- making an Executable JAR File ...PLEASE HELP [/b]

    Hi all,
    I Trying to deploy a simple J2SE app by making an Executable JAR File, however I don'r understand how to add a valid value to the Main Class field.
    I followed the following description within Jdev, after creating a Deployment Profile:
    Deploying an Executable JAR File
    You can make your simple archive or J2EE Client Module into an executable JAR file that you can launch with the java command.
    To deploy an executable JAR file:
    1) Select and right-click the simple archive or client icon in the Navigator to display the context menu.
    2) Choose Properties.
    3) Click JAR Options in the tree.
    4) Select Include Manifest File (META-INF/MANIFEST.MF).
    5) In the Main Class field, enter the fully qualified name of the application class that is to be invoked.
    7) Click OK.
    8) Launch the executable JAR file from the command line:
    java -jar myapp.jar
    where myapp represents your JAR file name.
    Like I said it's in 5) I'm doing something worng
    My deploy-file (PlotPrint.deploy) looks the following:
    <?xml version = '1.0' encoding = 'windows-1252'?>
    <client-deployment xmlns="http://xmlns.oracle.com/jdeveloper/903/deploy/j2ee-client-jar" nselem="client-deployment" class="oracle.jdeveloper.deploy.jar.ClientProfile">
    <archiveOptions>
    <additionalManifests/>
    <compressed>false</compressed>
    <hasManifest>true</hasManifest>
    <mainClass>PlotPrintClient</mainClass>
    </archiveOptions>
    <archiveTargetPlatform/>
    <cdaSettings>
    <additionalArchives/>
    <afterFilters/>
    <beforeFilters/>
    <duringFilters/>
    <selectedArchives>
    <archives>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/jdbc/lib/classes12.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/jdbc/lib/nls_charset12.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/jdev/lib/jdev-rt.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/soap/lib/soap.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/lib/xmlparserv2.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/jlib/javax-ssl-1_2.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/jlib/jssl-1_2.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/j2ee/home/lib/activation.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/j2ee/home/lib/mail.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/j2ee/home/lib/http_client.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/webservices/lib/wsdl.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/lib/xmlparserv2.jar!/"/>
    <Item protocol="jar" path="file:/C:/Oracle9i_Jdev/jdevbin/lib/xmlcomp.jar!/"/>
    </archives>
    <selectionMode>0</selectionMode>
    </selectedArchives>
    </cdaSettings>
    <defaultConnection class="java.lang.String"/>
    <deployClientMaxHeapSize/>
    <earURL path="deploy/PlotPrint.ear"/>
    <enterpriseAppName/>
    <jarURL path="deploy/PlotPrint.jar"/>
    <profileDeps/>
    <properties/>
    <selectedProjectFiles>
    <autoInclude>true</autoInclude>
    <deploySourceAs>0</deploySourceAs>
    <files class="[Ljava.net.URL;"/>
    <selectionFilters/>
    </selectedProjectFiles>
    </client-deployment>
    I using Oracle9i Jdev, but have also tried it un the new Jdev 10g version, but the same problem everytime:
    Error message: Could not find the main class: program will exit.
    Please help I really need this to work
    Message was edited by:
    MHCI
    Message was edited by:
    MHCI

    Yes that was the problem so I have changed the element mainClass to
    <mainClass>xxPlotPrint.MHCI.PlotPrintClient</mainClass>
    But now another probem has occured, since I get the following error when run the jar-file (When I run the app from within Jdev there are no warnings or errors).
    ----Batch-file-----
    @echo off
    java -jar PlotPrint.jar
    echo Test Plot and Print Batch-file!
    pause
    ---- out to cmd ----
    Exception in thread "Thread-0" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
    at xxPlotPrint.MHCI.NNE35_worker.run(NNE35_worker.java:42)
    at java.lang.Thread.run(Unknown Source)
    Exception in thread "Thread-2" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
    at xxPlotPrint.MHCI.OCEJobDB_worker.run(OCEJobDB_worker.java:41)
    at java.lang.Thread.run(Unknown Source)
    Exception in thread "Thread-1" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
    at xxPlotPrint.MHCI.NNE27_worker.run(NNE27_worker.java:42)
    at java.lang.Thread.run(Unknown Source)
    It's the same problem in all three exceptions (the exactly the same code), however I was a bit to quick this is the line (the line before I process the xml doc :-)
    import org.w3c.dom.Element;
    responseData = new Vector();
    responseData = test_NNE35_SOAP.getXSDdoc();
    Element elements = ( (org.w3c.dom.Element)responseData.elementAt(0) ); (line 42)
    Please help I don't understad why I receive this exception, since I included the xmlparserv2.jar file, and don't even have a workaround :-(

  • NoClassDefFoundError when attempting to execute jar file

    I've created a jar file for an application that uses a 3d party jar file -comm.jar
    I jarred this jar into my new jar and I get the following error when I attempt to execute the jar: "Exception in thread "main" java.lang.NoClassDefFoundError: ColorScan"
    where ColorScan is the class containing main() (as indicated in MF file)
    I believe this is a classpath problem, however, should this problem exist if the classpath includes the location of ColorScan and the 3d party jar file (comm.jar)???
    I'm running on Windows 2000
    thanks

    PROBLEM RESOLVED:
    To include an existing jar file in a newly created jar file FIRST extract the contents of the existing jar file
    use the command:
    jar -xvf comm.jar
    where comm.jar is the name of the jar file you wish to extract from. This operation may create subdirectories off of the directory where the jar file to be extracted exists (depends on jar file and what's in it)
    eg.
    myJarDirectory/javax/comm/*.class
    myJarDirectory/com/sun/comm/*.class
    create a Manifest.MF file in the directory where you want to create a new jar file (make sure the extracted .class files from the existing jar file, or the subdirectories that contain them, are located in this directory also).
    the manifest file looks like this (watch case!)
    Manifest-Version: 1.0
    Main-Class: MyMainClass
    Created-by: Joe Developer
    where MyMainClass is the name of the class file that contains your main()
    Make sure all of your class files (from your program and the original jar file) are located in the same directory (with the Manifest.MF file)
    Run the following command to create a new jar file
    jar -cvfm cs.jar Manifest.MF *.class javax com
    where javax and com are the base subdirectories extracted from the original jar file
    and cs.jar is the name of your new jar file.
    Once this jar file is created you can just click on the icon and run the app from anywhere
    that's all

  • Executable JAR file and subdirectories, locale...

    Hi:
    I have created an executable jar file. In my program, I have to display an icon in which I stored in a subdirectory. This subdirectory is also archived into the jar file. However, as the application runs, it can't find the icon! I have to seperately create a subdirectory (with the same name and structure as the archived directory) for my program to display the icon properly! Does anybody know how I can use the archived subdirectory directly?
    Also, the executable jar file has a problem of using the proper locale... Does anyone know how to get around this?
    Thanx in advance for helping me out = )

    Thanx! It worked!
    Does anyone know the answer to my second problem, namely, using the correct locale to display characters? When I run the application on the unarchived class file it works fine. However, when I run the application double-clicking the executable jar file icon, the characters are displayed incorrectly, which I assume to be problem with the locale...

  • Executable jar files and manifest file

    Hi,
    I have the following files in a folder named: Test
    a.jar
    b.jar
    c.jar
    Driver.class
    Driver.mf
    Here, MainClass is the main executable class that uses a,band c.jar files. This is how my MainClass.mf file looks:
    Manifest-Version: 1.0
    Main-Class: Driver
    Class-Path: a.jar b.jar c.jar
    Now I use the following command to make one executable jar file:
    jar cmf Driver.mf DriverMain.jar *
    It creates an executable jar file named : DriverMain.jar
    Now I copy the executable jar file (DriverMain.jar) into a different folder named: RunTest
    and double click it, doesn't work.
    my question is:
    what am I doing wrong? Any special characters needed in my Driver.mf file (space/newline/etc)?
    What I am trying to get is: One execuatble jar file so I can just double click to run it, and that single executable jar file will have all the necessary jars in it (i.e. a.jar, b.jar, c.jar in this case)
    Anyone please help!
    Thanks
    -Ron

    Rony,
    Sorry to disappoint, but you can not use embeded jar/zip files within an executable jar. The JDK sadly for some reason decided this was not a useful idea, so developers like me who want to distribute plugins with thier own dependencies have one of two choices. You either have to unzip the jar file that contains the embeded jars to a directory, then run your primary exectuable jar, OR you have to write a custom classloader that your "launcher" creates then loads the embeded jar files.
    The best thing to do is either jar up a, b, c and driver into one jar, so that it works, if you can legally do this. A lot of 3rd party libraries may not allow this per their license. Otherwise, another choice is to use a free installer or buy an installer that allows you to distribute a single exectuable installer program that will properly create the dir structure you need.
    As for the way it works, if you declare:
    Class-Path: a.jar b.jar c.jar
    the JAR loader code in the JDK looks in the root dir where your application was started for a/b/c jar files. They have to be on disk.
    If you want to place them in /lib, for example, you would havd a jar file like:
    Driver.class
    Driver.mf
    lib/a.jar
    lib/b.jar
    lib/c.jar
    When unzipped, your "root" dir would look exactly like the above, and your manifest would have Class-Path using ./lib/a.jar ./lib/b.jar ./lib/c.jar
    Anyway, there thus far isn't any way around this issue of embeding jar files in an executable jar file. You MIGHT be able to not specify any classpath, then in your Driver.class, create a new custom classloader that dervies from URLClassLoader, but in the findClass(), you get a ref to the .jar file that the classloader class is inside of, and from that use it to find the lib/*.jar files and add them to the classpath. For this to work, however, your Driver.class code should ALSO be contained in an embeded jar file that is loaded by this custom loader. The only thing Driver.class would contain (and I would rename it to something like Launcher) is the code to create the custom classloader.
    It's fun stuff, but a little bit of work to make it work. You can infact make it work! I may yet one day take our plugin engine custom loader and create a way for this to work!

  • Executing JAR Files Windows 98 and XP

    Hey can someone please help me.
    I made an executable jar file the extract contents of a jar file. In windows XP it runs fine, however in Widnows 98 it does not, has anyone come across this problem? Any info would be greatly appreciated.
    Thanks

    Well When I try, On a windows XP machine it shows the progress bar I made, and extracts all the files to the c drive. On Windows 98 It shows the progress bar but will not extract the files to the c: drive folder.
    The program simply gets all the files that are within the jar file, and puts their names in a Vector, then takes the names from the Vector and Extrats the ones that need to be extracted.
    Any other Ideas, I know this sounds very wacky thats why I am in need of help.
    Thanks for the ideas.

  • Class not found error while executing jar file

    I have written a java code in which i am connecting to database....when i run the class file using......java class_filename it works........
    but when i run the jar file using .......java -jar jar_name ...yi gives me the error as 'Class not found'......
    can anybody help me please......
    Thanks and Regards,
    Siddhesh

    that class is part of jar file.....in classpath i have included zip files for jdbc ...to get databse connection.....and the jar being executed ................still it gives the error ClassDefnotfound which i think is due to database connection problem.....the exception is raised while.....
    Class.ForName("Driver_name").newInstance();/ This driver is present in zip files that i have added in classpath....
    above statement doesn,t giv error while i execute the class file............but exception while executing jar file only..
    Thnaks and regards,
    siddhesh

  • Executable JAR file: Could not find the main class.

    Hello,
    I have a problem with making an executable JAR file.
    I have written a JAVA program that consists of five different classes of which User.java is the main class and I have saved a text document with Main-Class: User and a blank line after that.
    If I try:
    jar cmf MainClass.txt User.jar User.class Beheerder.class Operator.class Manager.class MaakVisueelSchema.class
    it makes a executable jar file which actually works! :)
    But when the Operator class trys to open the MaakVisueelSchema class the screen stays blank.
    I can run MaakVisueelSchema with java MaakVisueelSchema.
    So I tried to make an executable JAR that consists only of MaakVisueelSchema, the same way as I did for User:
    Main-Class: MaakVisueelSchema
    jar cmf MainClass.txt MaakVisueelSchema.jar MaakVisueelSchema.class
    Then I get the error message:
    Could not find the main class. Program will exit.
    from the Java Virtual Machine Launcher.
    The big difference between MaakVisueelSchema and the other classes is that MaakVisueelSchema contains a PaintComponent method and an ComponentListener. Is it possible that one of those creates the error?
    Can anyone help me with this problem?
    Thanks in advance!
    Bye!

    Yes,
    I tried:
    jar xvf MaakVisueelSchema.jar
    and it returns:
    META-INF/
    META-INF/MANIFEST.MF
    MaakVisueelSchema.classN/G. You need to manually create a manifest file in a text editor, have it point to your main class, and enter it in your jar command as an argument.

  • Name of the JAR file

    can you please tell me,
    the name of the JAR file where this class "'com.ibm.ejs.ns.jndi.CNInitialContextFactory' " exists .
    Thanks

    in fact, i need to know the name of the JAR file
    because i am getting exceptions,
    ...classnotfound......i want to put that JAR file
    in the classpath to avoid classnotfound exception.Uhm... if the stuff is not in your classpath, then you have no choice but to run through your file system and look into every JAR you can find.
    so, if i get to know the name of the JAR file
    .....i would be able to remove the runtime error.Doubt it. Websphere doesn't care for your classpath, it uses its own. So if it doesn't find the class, it's not there.

  • Classpath is not working for executable jar file

    I have created executable jar file using following command where manifest file contains Main-Class and Class-Path entries.
    jar cvfm app.jar META-INF/* lib/* *.class
    So here app.jar contains my external jar's in lib directory
    but when I move app.jar to another directory, it's doesn't get my external files.
    I'm not getting this problem as these jar's are in app.jar
    class-path entry in manifest file:
    Class-Path: lib/abc.jar lib/xyz.jar
    Pls help me where it is getting fail?
    Thanks in advance,
    Vikas

    Java doesn't handle jars within jars. Your library jars need to be outside of the executable jar as in
    dist - main.jar
          - lib  -  lib1.jar
                 -  lib2.jar

  • Unable to execute JAR files

    I am unable to execute jar files and applets. Also, the Java Web Start and the Java Plugin Control Panel will not load. I have unloaded the JRE and installed other versions. To date I have loaded and unloaded the following:
    1.4.1
    1.4.2_01, _02, _04
    1.4.2_04 SDK
    But no change in the outcome.
    The javaw application starts but does not load the jar file.
    The applets and jar files run fine on other computers with any of those JRE/SDK's installed.
    Any suggestions would be helpful.

    Command line
    To create a JAR file >jar cvf filename.jar filename.class(s)
    To view the contents of a JAR file >jar tvf filename.jar
    To extract the contents of a JAR file >jar xf filename.jar
    To extract specific files from a JAR file >jar xf filename.jar archived-file(s)
    To run an application packaged as a JAR file
    >jar xvf filename.jar META-INF/MANIFEST.MF
    The above will place META-INF/MANIFEST.MF file in your working directory. Open this file in text editor. Under Version, type in
    Main-Class: filename
    example;
    Manifest-Version: 1.0
    Main-Class: SelectPurchase
    Created-By: 1.3.0 (Sun Microsystems Inc.)
    save file
    update META-INF/MANIFEST.MF file in jar file with;
    jar umf META-INF/MANIFEST.MF filename.jarto run jar file from command line;
    java -jar filename.jarTo run an application packaged as a JAR file
    (version 1.1) jre -cp app.jar MainClass
    (version 1.2 -- requires Main-Class
    manifest header)
    To invoke an applet packaged as a JAR file <applet code=AppletClassName.class
    archive="JarFileName.jar"
    width=width height=height>
    </applet>
    Does anyone know how to get the application to run by clicking on a Desktop Icon?

  • Determining the name of the jar file running from JAVA

    Hi everyone,
    This is probably a silly question, but how do you determine the name of the jar file running from within the jar file.
    E.g. If I'm running the jar file "Test.jar", then how can I get the String value "Test.jar" from within my program?
    Any help would be appreciated! Thanks:)

    I think that you can use the solution above to get the jar file resources associated with a particular jar file, and then use the JarFile class to get a manifest of whats in there, and then find your class?
    This is only off of the top of my head, and I haven't tested it. Let me know if it works

  • Can't Execute JAR file deployed from JDev 903

    Hi Everyone:
    I took the Oracle beginning Java class, and got a copy of JDeveloper 9, and started in on a project. The task was to create a small module that will allow users with no direct SQL access to change their Oracle passwords. The good news is that it runs great from within JDev; it catches exceptions cleanly, it handles multiple databases with no problems. The bad news is that I cannot get it to run from a batch file or command line (client is Windows). I have run 'setvars', I have copied the CLASSPATH from the run window in JDev, all to no avail.
    I am trying to deploy the app as an executable jar file. It's not meant to be an applet, because of the security implications.
    I would gladly RTFM, but the FM's that I have gloss over the deployment step in Jdev. I figure it's something obvious, but I just cannot figure it out.
    Any suggestions? Help a newbie...
    Thanks,
    Mike

    Hi Mike,
    I suggest you take a look at the log window of JDev, when you've successfully run your project.
    If the log window is not displayed, go to View | Log.
    Cut the command line, invoking javaw.exe, and paste it into your favorite editor.
    Change javaw.exe into java.exe, save your file as a bat file, go in a DOS console, and run the bat file.
    Let me know if it fixes your problem.
    Thanks,
    - Olivier

Maybe you are looking for

  • JSAPI - other languages than English; MIDlet - settings loading; JSAPI - al

    Hello! I need to create application which uses speech recognition. At first I thought about using CMU Sphinx (PocketSphinx or, possibly, Sphinx4). Later I thought about JSAPI. (Sphinx somehow uses JSAPI but I don't know what is the difference between

  • ITunes and Windows Media Player

    I just installed iTunes 8 on my PC with Vista and now I can't even open Windows Media Player. I did not change the default to iTunes and expected to still be able to use the Windows player. Any ideas for a fix?

  • Convert/Import TDMS to Excel

    So I have some LabVIEW code which logs to TDMS just fine.  I installed the TDMS Excel Add on to Excell 2007 and it imports the TDMS just fine and it looks good. What I would like do do now is instead of just having that TDMS file, I would also like t

  • Patch error in Weblogic server9.1 & 9.2

    :Ohi, i am using weblogic 9.1 and 9.2 on windows xp. when i click smart update to get patches i am getting an error: "an unknown error occured upgrading the patch client ", when i have a BEA support id and password , i am unable to see error in log f

  • How to create index - Fuzzy Matching and Stemming

    Hi, I have a problem with creating and index for fuzzy matching and stemming with my expect behavious I Have table customer and column sortname. This table ussally contains values in format: "Surname, FirstName" "Lenox, Carl" "Svensson, Max" "Hamlber