Java Path – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Sat, 25 Nov 2023 03:18:54 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Java Path – Java2Blog https://java2blog.com 32 32 Convert String to Path in Java https://java2blog.com/convert-string-to-path-java/?utm_source=rss&utm_medium=rss&utm_campaign=convert-string-to-path-java https://java2blog.com/convert-string-to-path-java/#respond Thu, 15 Sep 2022 18:53:42 +0000 https://java2blog.com/?p=20516 In this post, we will see how to convert String to Path in Java.

Using Paths’get() method [ Java 7 ]

To convert String to Path, use jva.nio.file.Paths's get() method. It is a static method.

Here is an example:

package org.arpit.java2blog;

import java.nio.file.Path;
import java.nio.file.Paths;

public class CreatePathFromStringMain {

    public static void main(String[] args) {
        String pathStr = "C:/temp/tempFile.txt";
        // Create path from string
        Path path = Paths.get(pathStr);
        System.out.println("Path => "+path);
    }
}

Output:

Path => C:\temp\tempFile.txt

You can also join multiple strings using Paths‘s get() method. It will join all the String with delimiter /.

Here is an example:

package org.arpit.java2blog;

import java.nio.file.Path;
import java.nio.file.Paths;

public class CreatePathFromStringMain {

    public static void main(String[] args) {
        // Create path from string
        Path path = Paths.get("C:","temp","tempFile.txt");
        System.out.println("Path => "+path);
    }
}

Output:

Path => C:\temp\tempFile.txt

Using Path’s of() method [ Java 11 ]

There is new method of() introduced in Java 11 which takes String as argument and provides the Path object.
Here is an example:

package org.arpit.java2blog;

import java.nio.file.Path;

public class CreatePathFromStringMain {

    public static void main(String[] args) {
        String pathStr = "C:/temp/tempFile.txt";
        // Create path from string
        Path path1 = Path.of(pathStr);
        System.out.println("Path => "+path1);

        // Using multiple Strings
        Path path2 = Path.of("C:","temp","tempFile.txt");
        System.out.println("Path Using multiple Strings => "+path2);
    }
}

Output:

Path => C:\temp\tempFile.txt
Path Using multiple Strings => C:\temp\tempFile.txt

That’s all about how to convert String to Path in Java.

]]>
https://java2blog.com/convert-string-to-path-java/feed/ 0
How to Get Temp Directory Path in Java https://java2blog.com/get-temp-directory-path-java/?utm_source=rss&utm_medium=rss&utm_campaign=get-temp-directory-path-java https://java2blog.com/get-temp-directory-path-java/#respond Thu, 15 Sep 2022 17:31:22 +0000 https://java2blog.com/?p=20491 In this post, we will see how to get temp directory path in java.

Get Temp Directory Path in Java

Using System.getProperty()

To get the temp directory path, you can simply use System.getProperty("java.io.tmpdir"). It will return default temporary folder based on your operating system.

Default temp directory path
Windows : %USER%\AppData\Local\Temp
Linux/Unix: /tmp

Let’s see with the help of example:

package org.arpit.java2blog;

public class GetTemporaryPathMain {

    public static void main(String[] args) {
        String tempDirPath = System.getProperty("java.io.tmpdir");
        System.out.println("Temp directory path : " + tempDirPath);
    }
}

Output:

Temp directory path : C:\Users\Arpit\AppData\Local\Temp\

By Creating Temp File and Extracting Temp Path

We can also create temp file and extract temp directory path using String’s substring() method.

Using java.io.File

To get temp directory path:

  • Use File.createTempFile() to create temp file.
  • Get absolute path using File's getAbsolutePath() method.
  • Use substring() method to extract temp directory path from absolute path.
package org.arpit.java2blog;

import java.io.File;
import java.io.IOException;

public class CreateFileAndGetTempDirMain {

    public static void main(String[] args) {
        try {
            // Create temp file
            File temp = File.createTempFile("temp_", ".tmp");

            String absTempFilePath = temp.getAbsolutePath();
            System.out.println("Temp file path : " + absTempFilePath);

            // Get temp directory path using substring method
            String tempDirPath = absTempFilePath
                    .substring(0, absTempFilePath.lastIndexOf(File.separator));

            System.out.println("Temp directory path : " + tempDirPath);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Temp file path : C:\Users\Arpit\AppData\Local\Temp\temp_10478314566038976912.tmp
Temp directory path : C:\Users\Arpit\AppData\Local\Temp

Using java.nio.File.Files

To get temp directory path:

  • Use Files.createTempFile() to create temp file.
  • Get absolute path using toString() method.
  • Use substring() method to extract temp directory path from absolute path.
package org.arpit.java2blog;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateFileNIOAndGetTempDirMain {

    public static void main(String[] args) {
        try {
            // Create temp file
            Path temp = Files.createTempFile("temp_", ".tmp");

            String absTempFilePath = temp.toString();
            System.out.println("Temp file path : " + absTempFilePath);

            String fileSeparator = FileSystems.getDefault().getSeparator();
            String tempDirPath = absTempFilePath
                    .substring(0, absTempFilePath.lastIndexOf(fileSeparator));

            System.out.println("Temp directory path : " + tempDirPath);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Temp file path : C:\Users\Arpit\AppData\Local\Temp\temp_6905627814634776014.tmp
Temp directory path : C:\Users\Arpit\AppData\Local\Temp

Override Default Temp Directory Path

If you want to override temp directory path, you can run java program with JVM arguments as -Djava.io.tmpdir=Temo_file_path

For example:
If you want set temp directory path as C:\temp, you can run java program with JVM argument as -Djava.io.tmpdir=C:\temp

That’s all about how to get temp directory path in Java.

]]>
https://java2blog.com/get-temp-directory-path-java/feed/ 0
Print Classpath in Java https://java2blog.com/print-classpath-java/?utm_source=rss&utm_medium=rss&utm_campaign=print-classpath-java https://java2blog.com/print-classpath-java/#respond Tue, 08 Feb 2022 08:30:52 +0000 https://java2blog.com/?p=19251 In this post, we will see how to print CLASSPATH in java.

💡 Outline

You can use System.getProperty("java.class.path") to get ClassPath in java.

String classpath = System.getProperty("java.class.path");
   String[] classPathValues = classpath.split(File.pathSeparator);
   System.out.println(Arrays.toString(classPathValues));

What is CLASSPATH in java

CLASSPATH variables tell application to location where it should find user classes.

Default CLASSPATH is current working directory(.). You can override this value using CLASSPATH variables or -cp command.

how To Print ClassPath in Java

You can extract ClassPath from java.class.path using System.getProperty() and split its paths using String’s split() method.
Using System.getProperty()

package org.arpit.java2blog;

import java.io.File;

public class PrintClassPathUsingSystemGetPropery {

    public static void main(String[] args) {
        String classpath = System.getProperty("java.class.path");
        String[] classPathValues = classpath.split(File.pathSeparator);
        for (String classPath: classPathValues) {
            System.out.println(classPath);
        }
    }
}

Output:

C:\Users\Arpit\IdeaProjects\JavaPrograms\target\classes
C:\Users\Arpit\.m2\repository\org\jsoup\jsoup\1.13.1\jsoup-1.13.1.jar
C:\Users\Arpit\.m2\repository\com\opencsv\opencsv\5.3\opencsv-5.3.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-lang3\3.11\commons-lang3-3.11.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-text\1.9\commons-text-1.9.jar
C:\Users\Arpit\.m2\repository\commons-beanutils\commons-beanutils\1.9.4\commons-beanutils-1.9.4.jar
C:\Users\Arpit\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar
C:\Users\Arpit\.m2\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-collections4\4.4\commons-collections4-4.4.jar
C:\Users\Arpit\.m2\repository\org\apache\commons\commons-math3\3.6.1\commons-math3-3.6.1.jar
C:\Users\Arpit\.m2\repository\com\google\guava\guava\14.0.1\guava-14.0.1.jar

how To Print Path of Java Running Program

If you want to print current working directory i.e. directory where program was started, you can print absolute path using new File(".").getAbsolutePath()

package org.arpit.java2blog;

import java.io.File;

public class PrintCurrentWorkingDirectory {

    public static void main(String[] args) {

        String absolutePath = new File(".").getAbsolutePath();
        System.out.println(absolutePath);
    }
}

Output:

C:\Users\Arpit\IdeaProjects\JavaPrograms\.

As you can see, this program prints current working directory where program was started.

Frequently Asked Questions

1. how To Print CLASSPATH on Windows?

You can print ClassPath on window using echo command as below:

C:> echo %CLASSPATH%

2. how To Print CLASSPATH on Linux?

You can print ClassPath on Linux with echo command as below:

% echo $CLASSPATH

3. What Is Default CLASSPATH in Java?

Default CLASSPATH is current working directory in java.

That’s all about How to print CLASSPATH in java.

]]>
https://java2blog.com/print-classpath-java/feed/ 0
How to set java path in windows 10 https://java2blog.com/how-to-set-java-path-windows-10/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-set-java-path-windows-10 https://java2blog.com/how-to-set-java-path-windows-10/#respond Sun, 06 Dec 2020 18:42:40 +0000 https://java2blog.com/?p=11165 In this post, we will see about how to set java path in windows 10 using cmd. To set java path, you need to first understand about JAVA_HOME and how to set JAVA_HOME in windows 10.
Let’s first understand about JAVA_HOME and then we will see how to set JAVA_HOME in windows 10.

What is JAVA_HOME?

JAVA_HOME is environment variable which contains installation directory of Java development kit(JDK) or Java Runtime environment(JRE). This environment variable is setup at operating system level.

JAVA_HOME=C:\Program Files\Java\jdk-11.0.9

Why do you need JAVA_HOME?

JAVA_HOME environment variable points to directory where java is installed on your system, so many java based applications such as TOMCAT use JAVA_HOME environment variable to locate java executables.

How to set JAVA_HOME in Windows 10?

  1. Locate JDK on your machine.
    • If you are using 64-bit java, then it will be in C:\Program Files\Java\
    • If you are using 32-bit java, then it will be in C:\Program Files (x86)\Java\
  2. Open windows search, type environment and click on Edit the system environment variables.

    Edit environment variable in java

  3. In System properties dialog, go to Advanced tab and click on button Enviroment Variables.

    Click on Edit environment variable

  4. In System variables, click NEW... button to add JAVA_HOME environment variable.

    Add new environment variable

  5. Provide variable name as JAVA_HOME and value as Java installation directory.

    Set Java home environment variable

Set java path in window 10

Now let’s see how to set java in path environment variable in windows 10.

  1. In System variable window, locate Path and click on Edit....

    Click on edit

  2. Double click on the empty row and add %JAVA_HOME%\bin.

    Set java path in windows 10
    Here, % symbol is used to locate JAVA_HOME environemt variable and \bin provides location for java.exe and h=javac.exe

  3. Close the command prompt and launch again.
  4. Check java version as below:

    Check java version

That’s all about How to set java path in windows 10.

]]>
https://java2blog.com/how-to-set-java-path-windows-10/feed/ 0
Difference between PATH and CLASSPATH in java https://java2blog.com/difference-between-path-and-classpath-in-java/?utm_source=rss&utm_medium=rss&utm_campaign=difference-between-path-and-classpath-in-java https://java2blog.com/difference-between-path-and-classpath-in-java/#respond Sun, 22 May 2016 18:45:00 +0000 http://www.java2blog.com/?p=198

In this post , we will see differences between PATH and CLASSPATH in java.

Let me provide simple definition about PATH and CLASSPATH.

PATH :

This is environment variable which operating system uses to locate executable such as javac, java, javah,jar etc.
For example: bin directory of jdk has all the executable such as javac,java so we can set upto bin folder.

"C:Program FilesJavajdk1.7.1bin"

CLASSPATH:

This is environment variable which java virtual machine (JVM) uses to locate all classes which is used by the program.
For example: jre/lib/rt.jar has all java classes and you also need to include jar files or class file which is being used by program.

"C:Program FilesJavajre1.7.1jrelibrt.jar"

PATH vs CLASSPATH:

Parameter
Path
classpath
Locate

It allows operating system to locate executable such as javac, java

It allows classloader to locate all .class file used by program
Overriding

You can not override path variable with java setting

You can override classpath by using -cp with java,javac or class-path in manifest file.
Inclusion
You need to include bin folder of jdk (For example jdk1.7.1/bin)
You need to include all the classes which is required by program
Used by
Operating system
java classloaders

You can go through top 50 core java interview questions for more such questions.

]]>
https://java2blog.com/difference-between-path-and-classpath-in-java/feed/ 0