5

I want to start a .NET application (compatible with Mono) from the context of a Java environment.

My guess would be that I'd have to somehow figure out if mono is installed, find the location and start that using the .NET application path as a parameter.

But what is a robust way to do it? Or is there a better way?

Perhaps I should clarify the context: the Java part is running as a plugin in an environment with limited interaction possible, so I would really prefer to find a way without having to need a configuration file or an user-interface.

Davy Landman
  • 15,109
  • 6
  • 49
  • 73

3 Answers3

1

If you already have .net/mono already installed and you can double click on the exe file and it runs, then you could just use Desktop.open()

As easy as:

Desktop.getDesktop().open(file);

See here for more details: Using the Desktop API in Java SE 6 EDIT

I had to boot my Linux box where I have a mono application and this worked just great:

class Launch { 
   public static void main( String ... arg ) { 
      new ProcessBuilder("/usr/bin/myapp").start();
   }
}

Repeat, worked just great!

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • well this depends on your mono install, on my Linux system, you have to call mono and pass the app as argument. – Davy Landman Jan 19 '11 at 21:40
  • 1
    :-o Oh, in that case you just have to: `new ProcessBuilder("/path/to/mono", "app").start();` and that's it. You cannot just guess where it could be installed. If you don't want to use a config file, then just hard code it. As matter of fact, if the executable is in your $PATH invoking `new ProcessBuilder("monoexecutable", "app").start();` should be enough. Have you tried that already? – OscarRyz Jan 19 '11 at 22:39
  • +1 for mentioning how simple this could be if the mono executable is expected to be on the PATH. This should be preferred to having hard-coded paths in the Java app. – Jesse Webb Feb 04 '11 at 20:57
0

Interesting question! The answer is not that straightforward anyway

If you are running Linux

you definitely need Mono to run a CLR executable

If you are running Windows

you can run the CLR executable via shell OR use a local Mono installation (since Mono runs on Windows too), but there are very few reasons to run Mono on Windows

So, I think you should first detect the OS in Java (please don't ask me how to), and then if you are running Linux find Mono.

Mono's main executable is usually in /usr/bin, but is always in the $PATH so you can run mono Executable.exe from console. If you can't run such a command in Java, or Mono is not in path, then you need to run whereis mono to get the path to the executable.

No other ideas.

Community
  • 1
  • 1
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
0

I don't know anything about .Net or Mono, but I would assume, it isn't much different to start a .Net application than regular one, or?

The class used to interface with the environment is java.lang.Runtime. It also allows you to execute commands. (.exec-methods)

In order to construct an appropriate command, the various functionality on java.lang.System will probably come in handy.

If this option is insufficient for the requirements of your particular situation, JNI might be your only option...

tveon
  • 579
  • 5
  • 24