Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: [SWT-App] Loading .swf files from a runnable .jar or external folder

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SWT-App] Loading .swf files from a runnable .jar or external folder

    Hey,

    I'm programming using the SWT Widget Library for Java in eclipse, and I'm designing a runnable Java application. I've got the application down, I just don't know how to load external .swf files from a folder on "ALL" computers. I can load Images from any computer, because I use the getResourceAsStream line of code. But the "import com.docuverse.swt.flash.FlashPlayer" "loadMovie(arg, arg)" only takes a String.

    So I did ClassName.class.getResource("blah.swf").getPath(); which gives you a string, I set it up, and running it on eclipse it can perfectly find the file in the package. When I export it, the runnable Jar I made cannot find the "blah.swf" inside of the .jar file.

    So there is my problem, how do I load my .swf files from within the .jar or from an external folder so clients can download along side the .jar executable application, so it can point to those swf files.

    Thankyou.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    loadMovie(arg, arg)" only takes a String.
    What is the contents of the String? Is it the path to the file on the local PC?

    So I did ClassName.class.getResource("blah.swf").getPath(); which gives you a string
    What was the contents of that String? A path to a file on the local PC?

    If the API only takes a String representing a path on the local PC, then you will need a way to have your input files on the local PC.

    Have you tried putting the input files in the same folder with the jar file and reading them from there?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    It would try to search in the .jar file so it would output a string like;

    "file://C://Desktop//application.jar!//blah.swf"

    So it knew where the flash file was, it just couldn't get to it, I tried replacing ".jar!" with an empty string "", and nothing. I ended up just now finding a solution, but it's kind of 'dirty'.

    What I did was make a folder "SWF" and .replace("application.jar!", "SWF"); so that it could find the blah.swf file.
    So it was: "file://C://Desktop//SWF//blah.swf"

    Which meant, where ever the application was being stored on the computer, the "SWF" folder had to be with it, otherwise if the SWF folder was in another directory and the application.jar was in another directory, the output string wouldn't be able to find the SWF folder and thus the .swf files. I'm fine with that though, but it's a real dirty solution. I'd rather have a better one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Why not put the swf files in the same directory with the jar file.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Yeah, that's what I did, so I have both my jar file and SWF files in the same folder. Ahh I guess I kind of solved my own problem.....well, I hope it helps anyone else with the same problem I have.

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Cross-posted here:
    java - SWT-App Loading .swf files from a runnable .jar or external folder - Stack Overflow

    The answer there sounds workable. What's being suggested is that you store the bytes of the SWF in the jar. When you want to use it, you obtain a local temporary filename (I think that would be safer than kludging-in a permanent one), copying the bytes to the local file and then loading the local file as your swf.

    Can you link this cross-post at SO? I can't do it, I'm not a member.

    edit:swt/swf confusion

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Hey Sean, would this be an accurate tutorial to read up on for what your saying Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O) ? Skip down to "Methods for Creating Regular and Temporary Files"

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Methods for Creating Regular and Temporary
    Yes, that looks about right, though it does remind me I should have a thorough read up on what's new in Java 7!

    I'd advise in favour of using methods that *don't* specify a path, allowing the JVM to choose where to put your temporary file. If you want to make portable applications, it's best to avoid depending on things like "working directory" and "file tree". The more you delegate your decisions to the JVM, the better the chance your code will work on diverse platforms.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Yeah, that's exactly what I wanted to go for. Ok, I'll read up on it tonight and get to you guys tommorow hopefully.
    Last edited by Jman; September 2nd, 2011 at 02:49 PM.

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    I've been up all night trying to learn to do this, and I am absolutely clueless. Can you please provide an example of what I'm trying to do. Because I'm guessing you want to take the files in the runnable jar file, place them in the temporary file that is created, and than point to the files in that temporary file. Which than goes to my next point, how am I suppose to point to those files in the temporary folder ? I'm guessing the same way I do <Classname>.class.getResource("blah.swf").getPatch (); ? So it's like <filename>.file.getResource("blah.swf").getPath( ); ? I been looking at tutorials on how to do this, but all of them show me how to write to the file using a String. fw.write("sdads"); <-- something like that, which doesn't help.

    EDIT: OHHH nevermind, I did the first part. I created a temp file (.swf) to hold the SWF file in my Jar. So I copied over the bytes to that temporary .swf file. The thing is, how do I find it now, I know its in the temporary folder, but in the code, how do I locate it.

    EDIT: I can use this, is this what everyone uses ; System.out.println("OS current temporary directory is "+ System.getProperty("java.io.tmpdir")); ? I CAN USE IT, but is it the proper way ?
    Last edited by Jman; September 2nd, 2011 at 03:24 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Which of these steps are you having trouble with?
    create a temp file and save its address (you'll use it in the last step)
    copy the "file"/resource from the jar file to the temp file
    use the path to the temp file in the loadMovie method call

  12. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Yeah I ended up getting it working. Thanks~ I just had to puzzle it together and look harder.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Yep, that's how programming can be sometimes

  14. #14
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Can you please provide an example of what I'm trying to do
    yeah sure, since I am hand-waving - but I can't do it instantly
    ... a few minutes pass. You have completed 75% of the requested code ...
    I ended up getting it working
    D'oh!

    package com.javaprogrammingforums.domyhomework;
     
    import java.io.*;
     
    import javax.swing.*;
     
    public class ImageButtonJarTempFile extends JFrame
    {
      public final static long serialVersionUID = 42l;
      public static class ResourceToFileButton extends JButton
      {
        public final static long serialVersionUID = 42l;
        public ResourceToFileButton(String resourcePath) throws Exception
        {
          super();
          // image bytes are in the resource. We're simulating
          // the situation where we can't use the bytes directy
          // using something like ClassLoader.getResourceAsStream()
          // because no handy constructor exists in 3rd party
          // API, so write a temp file and pass filepath to
          // lame constructor
          File fTmp = File.createTempFile("imgbtn", resourcePath.substring(resourcePath.lastIndexOf(".") + 1));
          System.out.println("About to get resource '" + resourcePath + "'");
          InputStream is = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(resourcePath));
          OutputStream os = new BufferedOutputStream(new FileOutputStream(fTmp));
          int aByte;
          while((aByte = is.read()) > -1)
            os.write(aByte);
          is.close();
          os.close();
          this.setIcon(new ImageIcon(fTmp.toString()));
        }
      }
      public ImageButtonJarTempFile() throws Exception
      {
        super("Image from temp file from jar");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(new ResourceToFileButton("likeaswf.png"));
        setSize(200, 200);
        setVisible(true);
      }
      public static void main(String[] args) throws Exception
      { new ImageButtonJarTempFile(); }
    }

    There you go ... like that. Dodgy Exception handling and byte-by-byte resource copy, but otherwise demonstrates the approach. I can't do the SWT version, don't have Eclipse, but it shouldn't be very different.
    Attached Files Attached Files

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Oh, excellent, thanks for the hard work man. Really appreciate it, I can tell you now there are similarities but also big differences I can see in our code. Next problem though, this one I can't find a solution to.

    I'm going to have about 30 tmp flash files. And so what I did is put the construction of those temp files in a loop, each randomly generated temp file is being overlapped with the named flash file in the package folder. I put the names of each flash file in an array string. Anyways, that isn't the point, because the Object FILE tempFile(named variable) is being replaced by the next flash file, I can't seem to delete the generated tmp flash files. I can only delete the last stored flash file with tempFile.delete();

    I have in a String array, the path of all the temporary files generated, where they are being stored on the computer:

    C:\Users\<PC-name>\AppData\Local\Temp\file2662437780951812601.s wf to
    Replacement C://Users//<PC-name>//AppData//Local//Temp//file2662437780951812601.swf <-- I did that using the replacement method, because I believe that's the proper way.

    I thought maybe I could, for a test, see if I can delete the first path in the array string by doing this.

    File f = new File(filePathWays[0]) //this is the first stored pathway for a flash file
                        f.delete();

    But I got nothing, and yes, the Input and Output streams are closed.

    So in short, I would like to know how to delete a file on a computer given the pathway/location to it. Is it even possible. The quick solution would to put each of the flash files in FILE object, and use the delete() method, but I want to be able to go through a loop with my array, and delete each file that way. Hence I'd like to know if I can delete a file on a computer by having the actual pathway to the file. I hope it's not too confusing.
    Last edited by Jman; September 2nd, 2011 at 06:06 PM.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: [SWT-App] Loading .swf files from a runnable .jar or external folder

    Can you write a short program that creates a temp file and then tries to delete it to demonstrate the problem?

    I can only delete the laste one with tempFile.delete();
    Can you save the values of tempFile in an array and then use those references to delete the files?

Similar Threads

  1. How to test all files in a folder
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 28th, 2011, 07:15 AM
  2. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 09:35 AM
  3. Runnable to WebService
    By fran_jo in forum Threads
    Replies: 1
    Last Post: April 12th, 2011, 05:12 PM
  4. Listing files in a folder in a .jar
    By kulan8 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 22nd, 2010, 08:18 AM
  5. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 09:53 AM