In this case, use FilenameUtils.getExtension from Apache Commons IO

Here is an example of how to use it (you may specify either full path or just file name):

import org.apache.commons.io.FilenameUtils;

// ...

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"
String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Maven dependency:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>

Gradle Groovy DSL

implementation 'commons-io:commons-io:2.6'

Gradle Kotlin DSL

implementation("commons-io:commons-io:2.6")

Others https://search.maven.org/artifact/commons-io/commons-io/2.6/jar

Answer from Juan Rojas on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-format
Java File Format | .java Extension - GeeksforGeeks
January 29, 2024 - A source code file written in the Java programming language is saved with the extension .java. .Java file extension typically contains Java code that can be compiled into bytecode and executed on the Java Virtual Machine (JVM).
Discussions

How come if a Java file is part of a package, I have to run it by doing "java File.java", but if it's not in a package, I have to do "java File"?
java File.java I don't think this is doing what you think it's doing. Recently, java allowed you to compile and run in the same step. That's what is going on here. If you pass java a .java file it will compile and then run it. Traditionally, all .java files went to javac and all .class files went to java (but omitting the extension). More on reddit.com
🌐 r/learnprogramming
4
3
November 10, 2021
&quot;What is the extension of java code files?&quot;
The correct answer is .java. Key Points Java code files have the extension .java. This extension indicates that the file contains Java source code, which More on testbook.com
🌐 testbook.com
1
1736
1 week ago
Help with setting up vscode for java on wsl
i tried to setup java on vscode, but i ended up using intellij. More on reddit.com
🌐 r/vscode
16
18
May 3, 2020
Problem with IntelliSense and Java
I had a similar problem with Java and intellisense and vscode complained about something called "classpath". I started to use the "Create Java Project" option from command pallete which comes from the Java Extension Pack and it just worked. Turns out vscode needs the project files to be placed in correct folders and needs .class files to get the intellisense to work correctly More on reddit.com
🌐 r/vscode
12
8
June 18, 2019
🌐
Visual Studio Code
code.visualstudio.com › docs › java › extensions
Java extensions for Visual Studio Code
November 3, 2021 - If you are looking for core Java development experience on Visual Studio Code (including Java code auto-completion, running / debugging / testing Java applications, Java project management, etc.), we recommend the Extension Pack for Java.
🌐
Apache OpenOffice
openoffice.org › dev_docs › source › file_extensions.html
List of File Extensions
This page lists the filename extensions, or suffixes, used in the source code.
🌐
Baeldung
baeldung.com › home › java › java io › how to get the file extension of a file in java
How to Get the File Extension of a File in Java | Baeldung
January 8, 2024 - As we can see, these five examples cover different cases. Their corresponding file extensions are following: static final String EXPECTED_1 = "java"; static final String EXPECTED_2 = ""; static final String EXPECTED_3 = "gitignore"; static final String EXPECTED_4 = "log"; static final String EXPECTED_5 = "";
🌐
Javadoc.io
javadoc.io › doc › de.alpharogroup › jcommons-lang › 5.2 › de › alpharogroup › file › FileExtension.html
FileExtension - jcommons-lang 5.2 javadoc
Bookmarks · Latest version of de.alpharogroup:jcommons-lang · https://javadoc.io/doc/de.alpharogroup/jcommons-lang · Current version 5.2 · https://javadoc.io/doc/de.alpharogroup/jcommons-lang/5.2 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/...
Find elsewhere
🌐
Christian Vallentin
vallentin.dev › blog › post › associate-file-extension-with-java-application
Associate File Extension with Java Application · Christian Vallentin
Tells that the program we want to start is a Java program. %~dp0 is converted to the parent path of the current Batch file. %1 – The first command-line argument of the Batch file (the double-clicked .awesome filename). Setting the title or turning off command prompting, doesn't actually matter as the console is only visible for a fraction of a second. Before we're done, we need to actually assign the .awesome file extension, to our .bat file.
🌐
IBM
ibm.com › docs › da › i › 7.4.0
Installing Java extensions - IBM Documentation
October 7, 2025 - Extensions are packages of Java™ classes that you can use to extend the functionality of the core platform. Extensions are packaged in one or more ZIP files or JAR files, and are loaded into the Java virtual machine by an extension class loader.
🌐
Apache
logging.apache.org › log4j › 2.x › javadoc › log4j-core › org › apache › logging › log4j › core › appender › rolling › FileExtension.html
FileExtension (Apache Log4j Core 2.26.0 API)
java.lang.Enum<FileExtension> org.apache.logging.log4j.core.appender.rolling.FileExtension · All Implemented Interfaces: Serializable, Comparable<FileExtension> public enum FileExtension extends Enum<FileExtension> Enumerates over supported file extensions for compression.
🌐
W3Schools
w3schools.com › java › java_files.asp
Java Files
If you don't know what a package is, read our Java Packages Tutorial. The File class has many useful methods for creating and getting information about files.
🌐
Wikipedia
en.wikipedia.org › wiki › Java_class_file
Java class file - Wikipedia
May 30, 2026 - A Java class file is a file (with the .class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM).
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Extension Pack for Java - Visual Studio Marketplace
Extension for Visual Studio Code - Popular extensions for Java development that provides Java IntelliSense, debugging, testing, Maven/Gradle support, project management and more
🌐
Reddit
reddit.com › r/learnprogramming › how come if a java file is part of a package, i have to run it by doing "java file.java", but if it's not in a package, i have to do "java file"?
r/learnprogramming on Reddit: How come if a Java file is part of a package, I have to run it by doing "java File.java", but if it's not in a package, I have to do "java File"?
November 10, 2021 -

So I've been working on some Java programs for a class and I use VSCode and run the programs in powershell. (So the file structure might not be properly set up since I'm not using a proper IDE)

But if the java program has package <folder_name> at the top, then to run the program in a terminal I do

javac File.java

java File.java

However, if I remove this package <folder_name> from the file, then I have to do

javac File.java

java File

Why is this? Nothing else about the folder structure or code changes, but in one instance I need to include the file extension when running the program, yet in the other instance I need to omit it.

🌐
Groovy
groovy.github.io › GMavenPlus › jacoco › org.codehaus.gmavenplus.util › FileUtils.java.html
FileUtils.java
* * @param file the file to get the extension from * @return the file extension */ public static String getFileExtension(final File file) { String fileName = file.getName(); int dotIndex = fileName.lastIndexOf('.'); return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1); } /** * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
🌐
Edureka Community
edureka.co › home › community › categories › java › what is the extension of java code files
What is the extension of java code files | Edureka Community
October 11, 2023 - What file extension is commonly associated with Java source code files, and could you ... explanation of its significance in Java development?
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-the-file-extension-in-java
How to get the file extension in Java? - GeeksforGeeks
February 20, 2024 - In Java, a File Extension refers to the characters after the "." in a file name. It denotes the file type. For example, in the file name document.pdf, the file extension is pdf.
🌐
Online Convert
online-convert.com › file-format › java
JAVA File Format Information
Files containing source code written in the programming language Java are saved with the JAVA file extension. These files can be further compiled into CLASS files using a Java Compiler.
🌐
LabEx
labex.io › tutorials › java-how-to-extract-the-file-extension-from-a-filename-in-java-414024
How to extract the file extension from a filename in Java | LabEx
This tutorial will guide you through the process of extracting file extensions in Java, covering the fundamental concepts and providing practical examples to help you master this technique.