3

I have script to get the java path from this command: readlink -f $(which java).

But it displays wrong path : /usr/local/bin/javavm

While I need to have this path : /usr/local/openjdk8/bin/java

I tried to give export command: export JAVA_HOME=/usr/local/openjdk8/, but the issue remains same.

I tried this post as well : https://stackoverflow.com/a/39691105/10220825

and this post : https://askubuntu.com/a/554052/905016

I also tried : sudo ln -s /usr/local/bin/javavm /usr/local/openjdk8/bin/java but it results, ln: /usr/local/openjdk8/bin/java: File exists and issue remain same.

I dont want to hardcode it, neither I want to use other command like which java or echo $JAVA_HOME as the script is getting the expected result in Linux but only problems with FreeBSD machine.

I also dont want to apply any parsing like awk or sed from the output of readlink -f $(which java).

Can someone suggest me how to change the value from readlink -f $(which java).

Rob
  • 14,746
  • 28
  • 47
  • 65
Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • `readlink` does *not* report the wrong path. Do you want to change the default path to something else? Try [`update-alternative`](https://linux.die.net/man/8/update-alternatives). – Matthieu Sep 04 '20 at 09:40
  • You might want to replace the `unix` tag by the actual Linux flavor you use (e.g `bsd`). – Matthieu Sep 04 '20 at 09:46
  • What are you actually trying to do here? Why are these paths significant? – Kevin Boone Sep 04 '20 at 09:48
  • (and the correct `ln` syntax is `ln -s [destination] `) – Matthieu Sep 04 '20 at 09:48
  • @Matthieu, I have tried with the same`ln` syntax, the result is same – Ratnesh Sep 04 '20 at 09:51
  • Then `rm && ln -s ` should do it. But again, it depends on your Linux flavor. `update-alternatives` is usually the preferred way. – Matthieu Sep 04 '20 at 09:57
  • @Matthieu, I am working on the FreeBSD machine and `update-alternatives is only Linux command..` – Ratnesh Sep 04 '20 at 10:14
  • Why do you think this is wrong? What isn’t working? What you’re seeing is the expected behaviour on FreeBSD, and everything should work fine. – Konrad Rudolph Sep 04 '20 at 10:24
  • @KonradRudolph, I am not able to change the java path from `javam` to `openjdk/binjava` path to read it from `readlink -f $(which java)` – Ratnesh Sep 04 '20 at 10:28
  • 1
    @Ratnesh That doesn’t answer my question. **Why** do you want to do that? – Konrad Rudolph Sep 04 '20 at 10:38
  • @KonradRudolph, Actually, I need to profile one Java application through one profiler but it seems profiler does not able to report application's related data with the javavm while doing the same thing with /usr/local/openjdk8/bin/java it reports the application's related data.. – Ratnesh Sep 04 '20 at 10:43
  • Note: FreeBSD is not Linux. Removed tag. – Rob Sep 04 '20 at 12:38

1 Answers1

5

What you’re seeing is the documented behaviour on FreeBSD.

If you want to select a specific Java version, set the JAVA_VERSION environment variable, e.g.:

JAVA_VERSION=8+ javac MyClass.java

If you want to find out what version is being run, set JAVAVM_DRYRUN=yes:

JAVAVM_DRYRUN=yes java

JAVAVM_DRYRUN=yes JAVA_VERSION=11 java
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214