Java paths get linux

Java get path of dir of java file where main is located

Could you explain why exactly you need the main file? Because it sounds like you think you’ve solved part of the problem, and want help to solve the rest. I cannot think of any scenario where you need the main file. Also, are we talking about the source file, or the compiled class file?

@Georgios Due to the way that the classpath works in the JVM you want to consider compiled java-files (foo.class) as read-only and off-limits. This implies that you do the same with source files (when compiling and running) as they are used to generate the class files. You should use a folder only for these files elsewhere from the sources. It can be located in your project folder if your version control is correctly configured. You may want to explain in more detail what you want to do and why.

You can get the path with a relatively simple way. Questions exist on SO. It is just in my opinion not the right way to do it

2 Answers 2

import java.io.File; public class Test < public static void main(String[] args) < String name = Test.class.getName().replace(".", File.separator); String path = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + name.substring(0, name.lastIndexOf(File.separator)); System.out.println(path); >> 

It worked! I still find it strange to do it like that. And do not really understand why this is the case.

@Georgios you are doing unusual thing. You have a resource next to a java class that is not good practice. you should put resource in resources directory and read them like this

This relies on the JVM having the current working directory set correctly. You cannot always assume that.

@Spara It is only for maven projects that the notion of separate resource and source folders was introduced.

@ThorbjørnRavnAndersen yes you are right and OP needs the solution on maven. I don’t really like this solution as l told OP it’s really bad practice and we should avoid it

Since the latter code was not working as expected, I updated it.

String canSubString = canonicalPath.substring(0,canonicalPath.lastIndexOf("/")); //+/- 1 character, just check the output. String classname = canonicalPath.substring( canonicalPath.lastIndexOf("/") + 1); String command = "find " + canSubString + " -name " + classname; Process p = Runtime.getRuntime().exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); System.out.println(in.readLine()); 

From here on just don’t read on.

You could call the commandline tool find within your code and let it only seek there, since we assume you actually know what the Main is called.

So call in your terminal to understand what it does:

Читайте также:  Hardware monitor for linux

Now, we need to combine it with Java.

Process p = Runtime.getRuntime().exec("find /home/aaa/dev/ -name 'name.java'"); 

You will still need to get the output though. Being not that used to calling commandline tools from Java, I’d simply write the output to a file

Process p = Runtime.getRuntime().exec("find /home/aaa/dev/ -name 'name.java' > /tmp/pathtomain" ); 

At this point you will have the path you look for in /tmp/pathtomain. The /tmp folder is emptied at system shutdown by default, so no need to delete the file if this is no sensitive information.

Finally you will need to read out from that file.

Scanner scanner = new Scanner(new FileInputStream(new File("/tmp/pathtomain"))); String path = ""; while(scanner.hasNext())

If the filename exists only once, then this will result in having a one-lined String. It will become more difficult, if you want to find some path to a Mainclass with some generic name like Main.

If you get more then one line you could consider regexing over it with whatever information you can extract from the code.

Источник

How to find path to java?

That tells the command java resides in /usr/bin/java.

$ ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 2009-01-15 18:34 /usr/bin/java -> /etc/alternatives/java 

So, now we know that /usr/bin/java is actually a symbolic link to /etc/alternatives/java .

Dig deeper using the same method above:

$ ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 31 2009-01-15 18:34 /etc/alternatives/java -> /usr/local/jre1.6.0_07/bin/java 

So, thats the actual location of java: /usr/local/jre.

You could still dig deeper to find other symbolic links.

export JAVA_HOME=$(dirname $(dirname $(update-alternatives --list javac))) 

To make this seemingly over done setting clearer, on my Ubuntu linux machine with open JDK 8 installed:

$ update-alternatives --list java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java $ update-alternatives --list javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 

but what we need is the path to the directory containing bin of the JDK. So ask for the location of javac and then use dirname twice.

See man update-alternatives for more.

Be aware of the possibility that people might have two JDK’s installed. To always use the first one as your JAVA_HOME you can use head : JAVA_HOME=$(dirname $(dirname $(update-alternatives —list javac 2>&1 | head -n 1)))

Starting from January 2019, the licensing model for Oracle Java has changed. PPAs such as ‘ppa:webupd8team/java’ used in many Java installation tutorials now become unavailable.

Here I would like to share how I installed Java 8 on Ubuntu 16.04, and set the Java path in terminal.

Installation

I followed the instruction on the official documentation to install Java with .tar.gz

Path setting

The instruction is also from the official documentations. The steps to set up Java path are much simpler here.

After performing all the steps, restart the terminal and run ‘java -version’ to verify installation.

Источник

Where can I find the Java SDK in Linux after installing it?

I installed JDK using apt-get install but I don’t know where my jdk folder is. I need to set the path for that. Does any one have a clue on the location?

Читайте также:  Linux with mac interface

14 Answers 14

This depends a bit from your package system . if the java command works, you can type readlink -f $(which java) to find the location of the java command. On the OpenSUSE system I’m on now it returns /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/jre/bin/java (but this is not a system which uses apt-get ).

On Ubuntu, it looks like it is in /usr/lib/jvm/java-6-openjdk/ for OpenJDK, and in some other subdirectory of /usr/lib/jvm/ for Suns JDK (and other implementations as well, I think).

For any given package you can determine what files it installs and where it installs them by querying dpkg. For example for the package ‘openjdk-6-jdk’: dpkg -L openjdk-6-jdk

I think readlink as given is most elegant (and efficient), but I suggest readlink -f $(which javac) — note the ‘c’ in case there is a strange mix of JDK and JRE on the same machine. The JDK includes a compiler (javac) and a JRE does not. However if path is not correct, which will fail and you can try this: find /usr/java -wholename ‘*ava/jdk*’ -prune as I note in a comment below.

will tell you which java implementation is the default for your system and where in the filesystem it is installed. Check the manual for more options.

@dequis, it’s an answer specific to debian, since the question mentioned apt-get and the distro to be Debian 2.6.26 . AFAIK, it should be the same for all debian derivatives.

should give you something like

This does not actually point to a full JDK. 1. It is a symlink, and even if you read the symlink, the binary is also not within a JDK. For example, if I run the command readlink -f $(which javac) it prints /usr/lib/jvm/java-8-oracle/bin/javac . That bin folder is NOT a JDK. General acid-base test to see if its a JDK is to see if the current $JAVA_HOME contains a path of lib/tools.jar . In the cast of /usr/lib/jvm/java-8-oracle/bin that is not true, therefore it is not a JDK.

On Centos / RHL This is what I prefer to find the JDK (if installed) find /usr/java -wholename ‘*ava/jdk*’ -prune But behavior depends whether you are talking about OpenJDK or Oracle Java and how it was installed in the first place.

This question will get moved but you can do the following

«find / -name ‘javac'» is less typing, but requires admin (root) privilege or you will get a lot permission denied messages.

Use find to located it. It should be under /usr somewhere:

When running the command, if there are too many «Permission denied» message obfuscating the actual found results then, simply redirect stderr to /dev/null

find /usr -name java 2> /dev/null 

Another best way to find Java folder path is to use alternatives command in Fedora Linux (I know its for Ubuntu but I hit this post from google just by its headline). Just want to share incase people like me looking for answers for fedora flavour.

Читайте также:  Sed linux точное совпадение

To display all information regarding java

alternatives --display java 

Three Step Process: First: open Terminal-> $ whereis java it would give output like this: java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

Second: ls -l /usr/bin/java It would give output like this: lrwxrwxrwx 1 root root 22 Feb 9 10:59 /usr/bin/java -> /etc/alternatives/java

Third: ls -l /etc/alternatives/java output is the JDK path: lrwxrwxrwx 1 root root 46 Feb 9 10:59 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

It’s /usr/local/java/jdk[version]

This question still seems relevant, and the answer seems to be a moving target.

On my debian system (buster):

> update-java-alternatives -l java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 

However, if you actually go look there, you’ll see there are multiple directories and symbolic links placed there by the package system to simplify future maintenance.

The actual directory is java-11-openjdk-amd64 , with another symlink of default-java . There is also an openjdk-11 directory, but it appears to only contain a source.zip file.

Given this, for Debian ONLY, I would guess the best value to use is /usr/lib/jvm/default-java , as this should always be valid, even if you decide to install a totally different version of java, or even switch vendors.

The normal reason to want to know the path is because some application wants it, and you probably don’t want that app to break because you did an upgrade that changed version numbers.

Источник

Java Paths.get() strange behavior on Linux

I am currently writing some code in which I work with files a lot. I implemented all file paths processing (concatenation, normalization, etc) using the Java 7 nio classes Paths and Path. On Windows everything works as expected however on Linux the Paths class behavior seems to be broken. For example the following code:

 System.out.println(File.separator); System.out.println(FileSystems.getDefault()); Path path = Paths.get("../dir1/", "\\dir2\\file1").toAbsolutePath().normalize(); System.out.println(path); if(path.toFile().exists())
\ sun.nio.fs.WindowsFileSystem D:\projects\dir1\dir2\file1 true 

but the same code on Linux Ubuntu 14.04 on both Java 1.7.0_79 (64 bit) and Java 1.8.0_60 (64 bit) leaves the path un-normalized:

/ sun.nio.fs.LinuxFileSystem /home/semi/dir1/\dir2\file1 

Also even if the file is at path /home/semi/dir1/dir2/file1 exists it is reported as non-existent by path.toFile().exists() . I looked a bit over LinuxFileSystem.java and WindowsFileSystem.java and it seems that on windows the path is checked for both / and \ characters (in WindowsPathParser.isSlash(char c) method). Shouldn’t the Linux implementation do the same? Is this a bug in sun.nio.fs.LinuxFileSystem implementation or am I doing something wrong? Also do you know any alternative to make sure Linux paths are parsed and normalized correctly (without doing all the parsing manually).

Источник

Оцените статью
Adblock
detector