Results 1 to 4 of 4

Thread: Java Program did not work properly when i doubleclick to run

  1. #1
    Join Date
    Oct 2013
    Beans
    2

    Java Program did not work properly when i doubleclick to run

    Dear All Ubuntian:

    i new to ubuntu (ubuntu 12.04LTS)and just started to learn java program.

    i confuce with java program , i create program , set it to executable file, it run properly on eclipse (IDE) and terminal(after i export it) , but when i doubleclick to run nothing happen, anyone know why?? code as below:

    <java code>
    import javax.swing.*;import java.awt.HeadlessException;
    import java.io.*;
    public class h {
    /**
    * @param args
    * @throws IOException
    * @throws HeadlessException
    */
    public static void main(String[] args) throws HeadlessException, IOException {
    // TODO Auto-generated method stub
    String a="";
    InputStream s=new FileInputStream("data.xml");
    int size=s.available();

    for(int j=0;j<size;j++){
    a=a+(char)s.read();
    }

    JOptionPane.showMessageDialog(null,a);
    }


    }
    <java code end>

    so i wrote another program to launcher this using Runtime.getRuntime().Exec(); method with a JOptionPane.showMessageDialog(null, "open h.jar").
    When i double click it didn't launch my h.jar but the message show up, when i run with terminal every thing work fine , can anyone help me with this(confuse big time)?Code as below:


    <java code>
    import java.io.IOException;
    import javax.swing.*;




    public class launcher {


    /**
    * @param args
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    JOptionPane.showMessageDialog(null, "open h.jar");
    @SuppressWarnings("unused")
    Process p=Runtime.getRuntime().exec("java -jar h.jar");
    }


    }
    <java code end>



    Thank

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Java Program did not work properly when i doubleclick to run

    A Java program is a class or a jar, but double clicking on it outside the IDE won't start it because the code in the class/jar should be executed by the Java interpreter (the "java" command) and your file explorer has likely no association between the jar/class types and java (and when it does, it is rarely useful).

    And when the whole code is in a JAR, java must be told which class carries the main(). So either:
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #3
    Join Date
    Sep 2013
    Beans
    11

    Re: Java Program did not work properly when i doubleclick to run

    This is because the program doesn't find your file "data.xml"

    When you double click on the jar file, it thinks it was launched from your home directory and looks there for the file.
    Copy the file to ~/ and your program will work.

    It would be much safer and more convenient to import the needed resource into your eclipse project
    and than load it with getClass().getRessource("/your_resources_package/data.xml")
    Last edited by ICanHasNick; October 27th, 2013 at 11:36 AM.

  4. #4
    Join Date
    Oct 2013
    Beans
    2

    Re: Java Program did not work properly when i doubleclick to run

    ICanHasNick, your solution work wonderfully.


    Thank

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •