How do i run a file that is inside a jar file?

Hi all,
Here's what I'm trying to do: on a Unix (Solaris) box, I have a shell script bundled in with my application in a jar file. All I want to do is run the shell script from inside my java app, but I'm having trouble doing so.
If i try the following code:
Process process = Runtime.getRuntime().exec("myproject/tools/scripts/myscript.sh");
int exitVal = process.waitFor();I get the following error:
java.io.IOException: myproject/tools/scripts/myscripts.sh: not found
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:52)
        at java.lang.Runtime.execInternal(Native Method)So then I did some searching and tried something like :
     ClassLoader cl = this.getClass().getClassLoader();
     URL the_file = cl.getResource("myproject/tools/scripts/myscript.sh");
     Process process = Runtime.getRuntime().exec(the_file);
     int exitVal = process.waitFor();but i get this error:
cannot resolve symbol
symbol  : method exec (java.net.URL)
location: class java.lang.Runtime
     Process process = Runtime.getRuntime().exec(the_file);So is that the right way i should be doing this? Should i convert the URL (the_file)
to something else before passing to exec()?
Any hints/help/suggestions would be greatly appreciated.
Thanks.

The trouble with your first attempt might be that your relative path to the shell script is not relative to where you think it is. You could try again with an absolute path to the script as a simple test. Assuming that works you can try a more permanent solution -- perhaps a startup script for the Java app that defines a base path or a Properties file.
You are exactly right about the problem with your second try. The exec method in all its forms expects a String as the first argument, so the URL is not going to help much, assuming that the URL is in fact non-null.

Similar Messages

  • Dont ask why! How do I call an applet that is in a jar file that is...

    Can someone tell me how I would call an applet that is inside of a jar file that is inside of a parent jar file?
    example call to an Applet
    <APPLET CODE="TimeApplet" ARCHIVE="time.jar" />what I want to do is something like
    <APPLET CODE="TimeApplet" ARCHIVE="parent.time.jar" />
    where parent is the top jar and time is the inner jar.any help or links to tutorials/articles would be most appreciated.
    if you ask why I wont answer.

    we are doing a signed applet and we only want the user to have to sign 1 time
    and we have lots of applets to display so we dont want to have them sign for
    each item.Sorry but that doesn't make much sense, niether does putting a jar file in a jar file.
    Do you want the "do you trust" question only one time? If so than you only have
    to sign all the jar files with the same key.
    You can load a jar file from your applet but it seems that these jar files will run
    in sanbox security no matter how you sign it:
    http://forum.java.sun.com/thread.jspa?messageID=3582952
    Asking why does make sense, I coud be asking how to climb a building without
    telling my objective is to get on the 10th floor.
    You would tell me how to climb the building an I'd probably do it and find out
    later I could just as well have taken the elivator.

  • How do I update a file in an Applet's JAR file from the Applet code

    Here's my problem.
    My applet is using a serializable history data in which I am storing in the applet's JAR file. When I run the applet, I read the file with "getResourceAsStream()" and run my program with that hist data. When my applet is closed, I need to update this file from my Applet's code and I dumfounded about how to do that.
    Is there any way to update a file in the Applet's JAR file through the Java Applet code? (i.e. OutputStream?).
    Would appreciate any advice people have.

    Just place a copy of the file on the local hard disk and update that. When you start the Applet you try to read from the hard disk. If the file exists then no problem otherwise copy it from the jar to the hard disk.

  • How do I read the images I bundled into the jar file?

    I just wrote an applet that uses
         Toolkit tk = Toolkit.getDefaultToolkit();
         tk.getImage("blah.gif");to load a bunch of image files. The class file and gif images were bundled into a jar file to minimize the number of connections the browser has to make to the server.
    The applet works fine in appletviewer but trips a java.security.AccessControlException: access denied (java.io.FilePermission select.gif read) when run in a browser. I know you normally use Applet.getImage(URL) to load images but wont that circumvent the JAR file and make a seperate connection to the server for each image? That would defeat the purpose of using a JAR file.
    How do I access the image files I bundled into the JAR file?

    From your Applet class, write the following:
    URL imgUrl = getClass().getResource("blah.gif");
    Image img = getImage(imgUrl);The returned URL will be from the Applet class ClassLoader, which will look into the specified archive (jar) file.

  • How to add multiple images to a JLabel from the jar file

    I want to add multiple images in a JLabel but the icon property only allows me to add one. I thought I could use html rendering to add the images I need by using the <img> tab, but I need to use a URL that points to the image.
    How do I point to an image inside the jar file? If I can't, is there another way to show multiple images in a JLabel from the jar file?

    Thanks, it works perfectly. It's a really smart way of fixing the problem too :)
    I also found your toggle button icon classes which is something I've also had a problem with.
    Thanks.

  • JNI: How to use FindClass to get a class in a Jar file?

    Is it possible to use the FindClass method of the JNI environment object to get a class that is in a jar file? My sample application was working when my class files were in the local directory. Now that I have replaced the class files with a jar file containing the classes, the app no longer works. I'd appreciate tips from anyone who knows how to do this.
    -Andreas

    Is it possible to use the FindClass method of the
    JNI environment object to get a class that is in a
    jar file? My sample application was working when my
    class files were in the local directory. Now that I
    have replaced the class files with a jar file
    containing the classes, the app no longer works. I'd
    appreciate tips from anyone who knows how to do this.It has nothing to do with JNI.
    The method uses the class path just like any other class loading situation in java. The reason it worked before is because your class path included the directory. It doesn't work now because either the jar is not in the class path or there is something wrong with the class path.

  • Program Scheduler that will work for jar files or scripts

    I am  looking for a program scheduler that I can execute jar files or scripts that run jar files.  Need scheduler programs every 10min between 7:00am and 5:00pm.
    Anyone know a good tool for this? I would appreciate any help.

    iCal

  • How can i get the path to config folder placed inside the jar file?

    Hi i have developed an RCP application using eclipse.
    In my application config directory is there.
    When i export my RCP application as JNLP Project the jar file is created which contains config folder inside it.
    When i download the application using java web start , how can i get the path to config folder placed inside the jar file?
    Will the config folder exists in local cache in my system?
    Help needed.
    -Deepak

    -- This works in CS6:
    tell application "Adobe InDesign CS6"
      set myDocument to active document
      set selectedRectangles to selection of myDocument
      set theGraphicsLink to file path of item link of (graphic 1 of (item 1 of selectedRectangles))
    --> "Macintosh HD:folder/folder/filename.tif"
    end tell

  • Can't print a PDF file that is within a ZIP file.

    Can’t print a PDF file that is within a ZIP file.  Get message “Windows cannot complete the transaction.  The destination file could not be created.”  If I save the file and extract the pdf it works.  I didn't have to go through this hastle before.  If I retry many times it will often work.

    ZIP files can be handled in many different ways.  If you have a program like WinZip or 7-Zip installed, they will handle it.
    If not, Windows will handle it.  The message you quote indicates that your situation is handled by Windows, which seems to have a problem with it.  Could be caused by recent Windows updates.
    Definitely not an Adobe Reader problem.

  • How to read, write file inside the JAR file?

    Hi all,
    I want to read the file inside the jar file, use following method:
    File file = new File("filename");
    It works if not in JAR file, but it doesn't work if it's in a JAR file.
    I found someone had the same problem, but no reply.
    http://forum.java.sun.com/thread.jsp?forum=22&thread=180618
    Can you help me ? I have tried this for all night !
    Thanks in advance
    Leo

    If you want to read a file from the JAR file that the
    application is packaged in (rather than a separate
    external JAR file) you do it like this ...
    InputStream is =
    ClassLoader.getSystemResourceAsStream("filename");Better to use
    this.getClass().getClassLoader().getResourceAsStream();
    From a class near to where the data is. This deals with multiple classloaders properly.

  • How to point to the HelpSet file which is inside a Jar

    Hi All,
    Can anyone please help me out on as to how I incorporate my help files (which are inside a jar) with JavaHelp. They are working fine when they are in a folder. But giving an error when i am trying to reference it from a jar file.
    Thanx in advance,
    Debopam.

    Hello,
    I have a little Example, and I hope this will help you.
    Melly
    * Opens the documentation in an JavaHelpViewer.
    protected void showDocumentation() {
         String jarFilePath = ".../test.jar";
         HelpSet hs = getHelpSet(jarFilePath);
         if (hs!= null) {
         HelpBroker hb = new HelpBroker(hs);
    hb.setDisplayed(true);
         else {
         System.out.error("Can't find the helpSet-file.");
    * Returns the HelpSet file which is in included into the given
    * jar file.
    * @param jarFilePath
    * @return the HelpSet file, or null, if the HelpSet file is not found.
    protected HelpSet getHelpSet(String jarFilePath) {
         HelpSet hs = null;
         try {
         String hsName = null;
         JarFile jar = new JarFile(jarFilePath);
         Enumeration entries = jar.entries();
         while (entries.hasMoreElements()) {
              ZipEntry entry = (ZipEntry) entries.nextElement();
              String entryName = entry.getName();
              if (entryName.endsWith(".hs")) {
              hsName = entryName;
              break;
         URL url[] = getURLs("file:" + jarFilePath);
         ClassLoader cl = new URLClassLoader(url);
         URL hsUrl = HelpSet.findHelpSet(cl, hsName);
         hs = new HelpSet(cl, hsUrl);
         catch (Exception exc) {
         System.out.error("Can't find a HelpSet file in: " +jarFilePath");
         hs = null;
         return hs;

  • Trying to down load a file that contains .exe and .bin files on Unity(gaming builder)

    Trying to down load a file that contains .exe and .bin files on Unity(gaming builder). can this be done on iMac. it is a video game? It works fine in windows computer? dont know if this limited information helps. Is there a program or something i need to buy or download? Thanks

    Hello,
    .exe files are Windows® executable files, so you'd need Windows Installed.

  • How Can i open Html file in a Browser from Jar file

    Hi
    i am having Html help files inside my Jar file ... if i use
    getclass().getRource("\lib\start.html");
    it is not opening ... so i have to ship seperate folders for html files along with jar files.... can anyone give the solution to have(open) html files inside the jar file and to open then in a default browser of any OS
    Regards
    Ganesan S

    the follwing method i have used to open html file ...
    so to access html file i am shipping resources folder with jar file ..
    private void openHtmlPages(String pageName) {
         String cmd[] = new String[2];
         String browser = null;
         File file = null;
         if(System.getProperty("os.name").indexOf("Linux")>-1) {
              file = new File("/usr/bin/mozilla");
              if(!file.exists() ) {
              }else     {
                   browser = "mozilla";
         }else {
              browser = "<path of iexplore>";
         cmd[0] = browser;
         File files = new File("");
         String metaData = "/resources/Help/Files/"+pageName+".html"; // folder inside jar file
         java.net.URL url = this.getClass().getResource(metaData);
         String fileName = url.getFile();
         fileName = fileName.replaceAll("file:/","");
         fileName = fileName.replaceAll("%2520"," ");
         fileName = fileName.replaceAll("%20"," ");
         fileName = fileName.replaceAll("jarfilename.jar!"," ").trim();
         cmd[1] = fileName;     
         try{
              Process p = Runtime.getRuntime().exec(cmd);
         }catch(java.io.IOException io){
                   //Ignore
    can anyone give me the solution..???
    Regards
    Ganesan S

  • Call class inside a jar file??

    Hello group,
    I have a class with a main mathod and this class is inside a .jar file. This jar file reside under my
    web-inf\lib or class-path directory in my application at Weblogic Server.
    How can I call this class by command line?
    I have to create a cliente to do it?
    Is it possible? I´m having problems because the weblogic server is using the jar file at the same time that I trying use it calling by command line.
    Where can I find more information?
    Message was edited by:
    Denimar

    Authcloud9981 wrote:
    Hi I have an executible JAR file that I can call from command line by using the command - "java -jar file.jar".
    Is there a way for me to call this JAR file inside a Java class in another Java Program?There can be more going on in a jar than just as a container for classes.
    The manifest defines the behavior. So to replicate the behavior you must do exactly that.
    As an example of that.
    [http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html#download]
    Additionally by running it in your application you must consider what the scope of that is. If you just load the classes directly then they will be in the system class loader and thus cannot be unloaded. Using a class loader solves that.

  • Include and call an .exe file inside my jar file

    Hi.
    I want to include an exe file and call it inside my jar file. this is the code.
    InputStream is = getClass().getResourceAsStream("/native/my.exe");
              int[] line = new int[is.available()];
              File myFile = File.createTempFile("my","exe");
              myFile.deleteOnExit();
              myFile.setExecutable(true);
              FileOutputStream out = new FileOutputStream(myFile.getAbsolutePath());
              int i = 0;
              int c;
              while((c=is.read()) != -1){
                   line=c;
                   i++;
              is.close();
              for(int j= 0;j<line.length;j++)
                   out.write(line[j]);
              out.close();
    So i store it in my temp folder. Than i run
    String myFileLocation = myFile.getAbsolutePath();
    p = rt.exec(myFileLocation);
    Everything goes fine, but the behaviour of the .exe file is not the same. I mean when i run the commands
    PrintWriter writer = new PrintWriter(p.getoutputStream());
    writer.println("command");
    the behaviour is not the same. What goes wrong.

    But to load the xml file I need to create a File objectIf this is true then you will not be able to load the XML file, since a jar entry is not a File object and cannot be represented as one.
    However if you get rid of whatever it is that loads your XML file and replace it by more standard JAXP processing, you should be able to parse an InputStream that reads from the jar entry. You know how to get that so it should not be a problem.

Maybe you are looking for

  • How to Insert Image in XML publisher Report

    Hi Forum, I have created rtf file for xml report and in this i inserted one Image. When run the request from apps environment and while checking the output in excel format, Image is not displaying.. Please advice me. Thanks & Regards, Nag.CH

  • Can't find drivers for Lenovo G50-30 for Windows 7 32-bit

    Hi,  I can't find drivers for Windows 7x32 bit for my new Lenovo G50-30. Looks like Lenovo support doesn't have them. Am I wrong?

  • How to get wide report

    hi, i m working on oracle reports 10g , i m making a reports which has many columns when i run report i see that columns r coming down , so how can i set my report to get all column on same line , like A B C D E F G H I J K i want like this A B C D E

  • Update BPEL Process

    Is there a way to update the default design-time property (expiration date) of a BPEL Process human task prior to it being instantiated via an API call? How would you update multiple human tasks' expiration date for a single BPEL Process instance? Th

  • Error when try to build listener project

    I am trying to build the listener project using VC++6. But I get such errors:<br />//==============================================================/<br />--------------------Configuration: Listener - Win32 Debug--------------------<br />Compiling res