1

Currently I am working on a .net core console app with a embedded jar. There is no such thing as a java console app, right? So I would want to run my jar file inside my console app. I know the .net process API but I don't know if there is an environment variable telling me the java runtime location so that I can execute it.

Registry keys are not cross-platform, please don't give answers using some Windows-only solutions, please.

625
  • 78
  • 6
  • `JAVA_HOME`? .... – zerkms Apr 13 '17 at 01:28
  • I thought that `JAVA_HOME` needs to be set by the user... – 625 Apr 13 '17 at 01:31
  • Traditionally JAVA_HOME is an environment variable you can reference to lookup the path where JAVA is installed. The Java program would be installed in JAVA_HOME/bin/java. Java programs can run as a console app, please see http://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.html – Jason Heithoff Apr 13 '17 at 01:32
  • It does not matter what sets it - a user or an installation routine. But you don't have a better way to find where java is installed. – zerkms Apr 13 '17 at 01:32
  • @Jason Oh... But I have already started ao it's no turn back :P – 625 Apr 13 '17 at 01:34
  • @zerkms So I should tell the user to set it if it does not exist? – 625 Apr 13 '17 at 01:35
  • Yep ........... – zerkms Apr 13 '17 at 01:36
  • 3
    @Jeremy there is no such thing as a registry in .net core... why mark as duplicate? :( – 625 Apr 13 '17 at 01:54
  • 1
    Unless really necessary, you should convert the JAR to equivalent C#, or use IKVM to run it. Detecting Java runtime would be painful to maintain, but you definitely can check how ANTLR or other projects do. – Lex Li Apr 13 '17 at 02:15
  • @Lex Java is usually (if not always) backwards-compatible. What's the problem? – 625 Apr 13 '17 at 02:29
  • The problem is that Java runtime is no longer available everywhere. Due to its security issues, many systems can be Java free. – Lex Li Apr 13 '17 at 03:05

1 Answers1

2

You can get environment variables in a cross platform way using .NET Core

Console.WriteLine("JAVA_HOME: " + Environment.GetEnvironmentVariable("JAVA_HOME"));

As everyone is pointing out, making a cross platform 'runner' is a bad idea in that its difficult to maintain.

JAVA_HOME isn't created by default upon install, and it will be a flaky interface, but you could make it work.

Digicoder
  • 1,835
  • 1
  • 12
  • 21