2

I have a java application. I'm using eclipse to write, compile and create a runnable .jar.

The program is used to discover OCF devices.

It uses UDP and multicast.

Multicast code

public static void sendMulticast(byte[] data) throws Exception{
        DatagramPacket pack = new DatagramPacket(data, data.length, mgroup, mport);
        msocket.send(pack);
    }
public static byte[] recieveMulticast(int timeout) throws Exception{
        DatagramPacket packet;
        byte[] data = new byte[AppConfig.ocf_buffer_size];

        packet = new DatagramPacket(data, data.length);
        msocket.setSoTimeout(timeout);
        msocket.receive(packet);

        return data;
    }

The code works when I start it from eclipse. It also works when I run the .jar from console on Linux.

But when I start it with a double click, it doesn't work.

When started from console, it finds my test device in less then a second. When started with a double click it doesn't find it ever.

I haven't tested it on Windows yet, but the problem remains on Linux all the same.

What is the difference when you start .jar from console or by double clicking? Why is it effecting messages on multicast?

I'm using "Package required libraries into generated JAR". I'm using java 1.7 in eclipse, and 1.8 on Linux, maybe thats the problem? But why does running it from console work? I would understand if I used sudo, but I didn't.

Invader Zim
  • 796
  • 2
  • 14
  • 39

2 Answers2

0

When you are running any jar from console, Console/Terminal knows which program responsible to run the any jar i.e

java -jar example.jar 

but when double-clicking environment, OS/GUI manager doesn't know default responsible program to run the jar. ( Same way when you try to open some unknown extension file, Operating system will ask you open with which program/application)

To make Java open .jar files per default (i.e. double click) right click on any .jar file to select Properties. In the following window select the "Open With" tab to see e.g. the follwing choice:

enter image description here

Rupesh
  • 1,636
  • 13
  • 18
  • Hmmmm... Ok, I looked at it. I have 4 java versions, open jdk and oracle, both 8 and 9. I tried all of them, none of them work. Btw. app did run, it just doesn't work on multicast – Invader Zim Jun 05 '17 at 11:07
  • check which version you are using from console. java --version & configure same version for double click, Also check which user you are using for console & double click – Rupesh Jun 05 '17 at 11:15
  • did that, didn't help. i maybe found the problem, will post an update – Invader Zim Jun 05 '17 at 11:21
0

The problem was in current location, system property

user.dir

This is the first function I call in my main. It doesn't work from eclipse, so I'll put an argument for disabling it (it will be disabled only during development).

static void setCurrentDir() throws URISyntaxException{
    String s;
    s = ESP8266_Configurator.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
    s = s.substring(0, s.lastIndexOf('/'));
    System.setProperty("user.dir",s);
}

I hope this helps someone. Code should be exported with extracted libraries, not packaged, otherwise it doesn't work.

Invader Zim
  • 796
  • 2
  • 14
  • 39