Java file does not open

[Solved] java.io.FileNotFoundException in Java with Examples

In this article, we will discuss how to solve the FileNotFoundException in java. This exception mainly occurs for the below reasons:

1. If the application tries to open a file, but the file is not present in the desired location.

2. While creating the file, if there is a directory with the same name as the filename then this exception occurs.

3. The file is located in the desired location but
a. The application has no permission to write to the file since the file has read-only permission, or,
b. The permission of the file does not allow any application to read the file.

FileNotFoundException is a Checked Exception

FileNotFoundException extends IOException. This exception will be thrown by FileOutputStream, FileInputStream, and RandomAccessFile constructors when the specified pathname does not exist.

Читайте также:  Input with text python

FileNotFoundException is a checked exception that must be handled by the application.

[Fixed] FileNotFoundException in Java with Examples

import java.io.*; public class JavaHungry  public static void main(String[] args)  FileInputStream fileInputStream = null; String fileName = "C:/Users/javahungry/abc.txt"; try  // Read the file from desired location. fileInputStream = new FileInputStream(new File(fileName)); // Read contents of file while(fileInputStream.read() != -1) System.out.println(fileInputStream.read()); > catch(IOException ext)  if (ext instanceof FileNotFoundException) ext.printStackTrace(); >else  System.err.println("Exception " + ext); > > finally  if(fileInputStream != null)  try  fileInputStream.close(); > catch (IOException e)  e.printStackTrace(); > > > > > 

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream. (FileInputStream.java:157)
at JavaHungry.main(JavaHungry.java:10)

Solution:

If the exception indicates the file is in an invalid path, then we have to provide a valid path. You have to make sure that path actually points to a file or directory that exists in your system.

2. If the file does not exist then the application will create it. However, if the file can not be created and there is a directory with the same name as the filename then the FileNotFoundException is thrown as shown below:

import java.io.*; public class JavaHungry  public static void main(String[] args)  FileOutputStream fileOutputStream = null; String fileName = "C:/Users/javahungry/abc.txt"; try  
fileOutputStream = new FileOutputStream(new File(fileName),true); // Write content to file fileOutputStream.write(3); > catch(IOException ext)  if (ext instanceof FileNotFoundException) ext.printStackTrace(); >else  System.err.println("Exception " + ext); > > finally  if(fileOutputStream != null)  try  fileOutputStream.close(); > catch (IOException e)  e.printStackTrace(); > > > > > 

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (Is a directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream. (FileOutputStream.java:213)
at JavaHungry.main(JavaHungry.java:10)

Solution:

If the exception indicates provided filename is a directory, then we have to provide a valid filename that does not conflict with the directory name.
Another solution is to delete the directory if it is not being used by the application.

3. Now we provide a filename as input that exists in our system but has no permission to read/write from our application. The following exception is thrown:

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream. (FileOutputStream.java:213)
at JavaHungry.main(JavaHungry.java:10)

Solution:

If the exception indicates the file does not have permission, then we have to provide the read/write permission to the file.

That’s all for today. Please mention in the comments in case you have any questions regarding how to solve FileNotFoundException in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry

Источник

JAR File Not Opening on Windows? Here’s How to Fix It

jar file not opening

JAR files, also called Java Archives files, are archives of Java class files. In a nutshell, JAR files are groups of files that can contain images, audio, visuals, etc., and can run as a single executable program when paired with the Java Runtime Environment.

They are predominantly used by Java developers for coding and programming. However, jar files refusing to open is one of the many issues that can happen while using them.

Why Does My JAR File Not Open?

Here are the possible causes for your jar file refusing to open.

  • JAR file set to open with other applications.
  • Java Runtime Environment having issues.
  • Incompatible JRE or JDK version
  • Incorrect Filename Extension

How to Fix JAR File Not Opening?

There are several methods you can use to fix JAR files not opening. However, before you begin, ensure the JAR file has the correct filename extension. To do so, go to the View tab and enable File name extensions. Make sure the JAR file has .jar at the end.

Make your way down the list of fixes to see which one works for you.

Set JRE as Default for JAR files

For the JAR files to be opened as a standalone program, you need to have the JAVA Runtime Environment. However, if the JRE is not set as the default application for opening jar files, your computer tries using different applications to open the file and fails to do so.

So, you should set the JRE as the default application for opening JAR files.

  1. Click on the specific Jar file you want to open and right-click it.
  2. Select Open with and Choose another app.
    jar file choose more
  3. Check mark Always use this app to open .jar files option.
  4. Click on More apps and Look for another app on this pc.
    look for another app on this pc

javaw

  • Navigate to C:\Program Files\Java\jre\bin. (This is the default installation path of Java. If you have installed it in another folder, go there instead. The JRE folder may also differ depending on your JRE version.)
  • Select javaw.exe and hit Open.
  • You can also enter assoc .jar=jarfile and ftype jarfile=»C:\(path to your javaw.exe)» -jar «%1» %* command in an Elevated Command Prompt to set JRE as the default application.

    For Example:
    ftype jarfile wp-block-heading»> Use Command Prompt

    You can also easily open jar files using command lines in the Command Prompt.

    jar opening command

    1. Launch Run Utility using Windows key + R key.
    2. Enter cmd to open Command Prompt.
    3. In Command Prompt, type cd and enter the directory to your .jar file. (If the .jar file is on another drive, you need to change the selected drive by entering the driver letter.)
    4. If you don’t know how to enter the folder directory in Command Prompt, you can simply drag and drop the folder from File Explorer to Command Prompt.
    5. Enter the Java -jar command.

    If your jar file is non-executable, the above method won’t work on opening it. You would need to use a different command.

    non executable jar file opening command

    1. In Command Prompt, enter the command java -cp app.jar com..
    2. If you were not the one to create the jar file and therefore do not know the package name and class name, you can look it up on the downloaded website.

    Setup a BAT File

    Another method you can use to open .jar files is by creating a .bat file scripted to open a particular file. This method skips the normal method of opening the .jar file and helps resolves issues like this.

    1. Right-click on the empty area in the folder where your jar file is located and choose New.
    2. Select Text Document and open the document as is.
      new text document
    3. Type in Java -jar
      jar file opening bat fi;e
    4. Go to File > Save as.
    5. Make sure the Save as type option is selected as All files.
    6. Name the text document anything you want, but include a .bat extension at the end.
      save bat file

    For non-executable .jar files, you can use the same command as above to create a .bat file that opens it.

    Use Compatible Java Version

    If you have multiple Java versions and are trying to open a jar file made in a different version of Java from the one you are using to open it, the jar file may not open. While Java is supposed to be backward compatible, you may still face this issue sometimes.

    To solve this, you must define a compatible version with which the jar file should open.

    jar file create shortcut

      Right-click your jar file and tap on Create shortcut.

    shotcut target

  • Right-click the shortcut and select Properties.
  • Next to target, enter the following: “C:\(path to your compatible jdk or jre version)” -jar “C:\(path to your jarfile.jar)”
    For e.g. «C:\Program Files\Java\jre1.8.0_341\bin\javaw.exe» -jar «C:\Users\Username\Downloads\myjarfile.jar»
  • Click Apply and OK.
  • Open the shortcut.
  • You can also use this method in the Command prompt or create a bat file to open jar files.

    Reinstall JAVA Runtime Environment

    The jar files can not start as a standalone program without JRE, so JRE must be working properly to open jar files.

    If there are any errors in JAVA Runtime Environment, you can reinstall a clean new copy to nullify any issues plaguing it.

    Follow the steps below to reinstall it:

    1. Access Settings from the Start menu.
    2. Navigate to Apps > Apps & features.
    3. Locate Java and click on options button on the left.
    4. Select Uninstall and confirm Uninstall.
      uninstall java 8
    5. Download the latest Java build from its official website.
    6. Run the setup file to install Java.
      install java 8

    Run Jarfix

    Jarfix is a legit software that many Java developers use to troubleshoot issues with jar files. So, you can run Jarfix to fix issues with your jar files, including issues like jar files refusing to open.

    It is a lightweight program and doesn’t use many system resources. However, be sure to download this program from a trusted website.

    I’m a tech enthusiast, and I’ve always been troubleshooting errors by myself for years. I also like keeping up to date with the latest technology and gadgets.

    Источник

    java.io.FileNotFoundException (Access is denied) – Causes and Fix

    java.io.FileNotFoundException (Access is denied) – Causes and Fix tutorial shows what are the possible causes and fix for java.io.FileNotFoundException (Access is denied) exception.

    How to fix java.io.FileNotFoundException (Access is denied) exception?

    There are several possible causes due to which you may encounter java.io.FileNotFoundException (Access is denied) exception as given below.

    1) Trying to open and read a directory

    You cannot open and read a directory like normal files. Trying to do that will result in the exception. Please see the below-given code example where I try to open a directory and try to read it.

    Make sure that you are not trying to open a directory for reading or writing.

    2) You do not have permission to read the file

    If you try to open and read a file for which you do not have the read permission, you will get this exception.

    Make sure you have permission to read the file before opening and reading it.

    3) Trying to overwrite a read-only file

    If you try to overwrite a read-only file either using stream or writer, you will get the “Access is denied” exception.

    Always check that if the file with the same name exists and it is not read-only before actually writing the file. If the file exists and it is read-only, make it writable as given in the below example.

    4) Trying to create a file in the root folder of the system drive in Windows

    In some versions of Windows, the system does not allow some users to write to the root of the system drive if they do not have the required privileges. Try to create a file in a subfolder, for example, C:/somedir/somefile.txt instead of the root “C:/somefile.txt”.

    5) File is being used by another process

    If the file is already opened exclusively by some other process, opening it for either reading or writing will cause java.io.FileNotFoundException (Access is denied) exception.

    Make sure that the file is not opened by any other program or process.

    This example is a part of the Java File tutorial.

    Please let me know your views in the comments section below.

    Источник

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