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 19 of 19

Thread: How can I have a jar file on my gui?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I have a jar file on my gui?

    Basically, on my GUI, I want to have a jar file also, so it'll look like this:

    How would I do that?

    Please keep in mind, I'm new to Jframe & GUI making, I normally use console, but this is a must to do this.


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    If i understand correctly, you want to run an app inside your app?

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    If i understand correctly, you want to run an app inside your app?
    Correct, how would this be done? (Code example wise).

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    package yourpackage;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class YourClass {
     
        public static void main(String[] args) {
            try {
                Process jarProcess = Runtime.getRuntime().exec(new String[]{"java", "-jar", "Path To Your .jar File"});
                jarProcess.waitFor();
                InputStream inputStream = jarProcess.getInputStream();
                byte[] inputByte = new byte[inputStream.available()];
                inputStream.read(inputByte, 0, inputByte.length);
                System.out.println(new String(inputByte));
            } catch (InterruptedException ex) {
                Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    You can try this, new String(inputByte) is the console output text of the selected .jar.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    package yourpackage;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class YourClass {
     
        public static void main(String[] args) {
            try {
                Process jarProcess = Runtime.getRuntime().exec(new String[]{"java", "-jar", "Path To Your .jar File"});
                jarProcess.waitFor();
                InputStream inputStream = jarProcess.getInputStream();
                byte[] inputByte = new byte[inputStream.available()];
                inputStream.read(inputByte, 0, inputByte.length);
                System.out.println(new String(inputByte));
            } catch (InterruptedException ex) {
                Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    You can try this, new String(inputByte) is the console output text of the selected .jar.
    That's only starting it...

    I can do that with getruntime, I want to load it on my JFrame, not as a regular executable.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    Ok so the one you want to load has a graphical interface or it just outputs to the console?

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    Ok so the one you want to load has a graphical interface or it just outputs to the console?
    Basically I want the applet to load on my graphical user interface like this.
    JFrame myFrame = new JFrame("test");
    myFrame.setSize(850,700);
    myFrame.setDefaultCloseOperation(WindowConstants.E XIT_ON_CLOSE);
    myFrame.setVisible(true);
    //Load jar on GUI here.
    Last edited by testingjava; June 29th, 2012 at 03:35 PM.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    Yes i understand that but are you trying to load another gui inside a gui or are you trying to load the output/console of the jar inside a gui?

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    Yes i understand that but are you trying to load another gui inside a gui or are you trying to load the output/console of the jar inside a gui?
    I suppose, I'm trying to load a .jar on my GUI (JFrame), in other terms a GUI in side a GUI I guess.

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    Im not sure if you can do this in Java, Im going to do some research on this, just out of curiosity why would you want to do this?

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    Im not sure if you can do this in Java, Im going to do some research on this, just out of curiosity why would you want to do this?
    To make a macro, for a game.

  12. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Still need help:/

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    Yes i know im still researching

  14. #14
    Junior Member
    Join Date
    Jun 2012
    Location
    Long Island, NY
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I have a jar file on my gui?

    Ok Java is bundled with a utility called AppletViewer

    import java.applet.*;
    import java.awt.*;
    public class Myapplet extends Applet{
    	String str;
    	public void init(){
    		str = "This is my first applet";
    	}
    	public void paint(Graphics g){
    		g.drawString(str, 50,50);
    	}
    }

    HTML Code:
    <HTML>
    <BODY>
    <applet code="Myapplet",height="200" width="200">
    </applet>
    </BODY>
    </HTML>
    C:\javac> javac Myapplet.java

    C:\javac>appletviewer Myapplet.html

    and the output is
    applet.gif

  15. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by cristian_ny95 View Post
    Ok Java is bundled with a utility called AppletViewer

    import java.applet.*;
    import java.awt.*;
    public class Myapplet extends Applet{
    	String str;
    	public void init(){
    		str = "This is my first applet";
    	}
    	public void paint(Graphics g){
    		g.drawString(str, 50,50);
    	}
    }

    HTML Code:
    <HTML>
    <BODY>
    <applet code="Myapplet",height="200" width="200">
    </applet>
    <br /><div style="z-index:3" class="shade" align="center"></div>
    
    <script type="text/javascript"><!--
    var _gaq = _gaq || [];
    _gaq.push(
    ['_setAccount', 'UA-5573417-2'],
    ['_trackPageview']
    );
    (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
    })();
    //-->
    </script>
    </BODY>
    </HTML>
    C:\javac> javac Myapplet.java

    C:\javac>appletviewer Myapplet.html

    and the output is
    applet.gif
    That's not what I want lol, I basically want another java applet running on that gui where it says 'this is my first java applet', get it? :/

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How can I have a jar file on my gui?

    The applet class extends the Panel class and an instance of the applet class could be added to any container.

    Create an instance of a JFrame, create an instance of the applet and add it to the JFrame. Call the appropriate applet methods like a browser would and you should be able to "execute" an applet in the GUI provided by a JFrame.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I have a jar file on my gui?

    Are you asking how to use the Swing components of one jar in a JFrame you have created? You need access to the API of the jar, to add the jar to your classpath, and then construct the appropriate components based upon the API and add them to your JFrame as you would any other component. If all you have is a jar with no documentation, you do not know the class file structure, and thus cannot construct the appropriate objects to add to your user interface.

  18. #18
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I have a jar file on my gui?

    Quote Originally Posted by Norm View Post
    The applet class extends the Panel class and an instance of the applet class could be added to any container.

    Create an instance of a JFrame, create an instance of the applet and add it to the JFrame. Call the appropriate applet methods like a browser would and you should be able to "execute" an applet in the GUI provided by a JFrame.
    Could you please give me an example?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How can I have a jar file on my gui?

    The code could be something like this:
    Applet anApplt = new Applet();  // Create an instance of the applet
    JFrame jf  = new JFrame();        // create frame to show applet in
    jf.add(anApplt);          // add instance to jframe
    anApplt.init();              // call applet's init
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  2. Replies: 4
    Last Post: February 14th, 2012, 03:28 PM
  3. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM