Issue
I'm trying loop through a menu with various options. Option 1 involves reading from a .json file.
But I keep getting this error:
Exception in thread "main" java.io.FileNotFoundException: (The handle is invalid)
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.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at com.malikrobinson.presentation.Main.main(Main.java:43)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:764)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:711)
at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:289)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 5.264 s
Finished at: 2020-10-05T16:17:35-04:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project YouDontNeedToKnow: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Here is the main class of my program. I am using Netbeans. I can update with the other classes if you need me to.
package com.substitue.presentation;
import com.substitue.business.Shift;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Scanner;
/**
*
* @author Notrelevant
*/
public class Main {
public static void main(String args[]) throws FileNotFoundException{
int choice;
String ifileName;
String ofileName;
String menu = "Shift UI\n--------\n1 – Read shift from file as JSON\n2 – Write shift to file as JSON\n3 – Show shift info on screen\n4 - Exit\nEnter Choice\n";
Shift first = new Shift();
//First prompting of the menu
Scanner keyboard = new Scanner(System.in);
System.out.println(menu);
choice = keyboard.nextInt();
while (choice != 4) {
switch(choice)
{
case 1:
//Print prompt
System.out.println("Enter Shift JSON Input Filename\n");
//Problem starts after this point apparently.
//Accept user input
ifileName = keyboard.nextLine();
//"Open file"
FileReader infile = new FileReader(ifileName);
//Call the method that reads from the file
first.readJSON(infile);
//Re-print the menu and prompt the user
System.out.println(menu);
choice = keyboard.nextInt();
break;
case 2:
//Write shift to file as JSON
System.out.println("Enter Shift JSON Output Filename\n");
ofileName = keyboard.nextLine();
PrintStream outfile = new PrintStream(ofileName);
first.writeJSON(outfile);
System.out.println(menu);
choice = keyboard.nextInt();
break;
case 3:
System.out.println(first.toString());
System.out.println(menu);
choice = keyboard.nextInt();
//Show shift info on screen
break;
}
}
}
}
I just can't figure out why the error shows up. I have the "throws FileNotFoundException" in there. Everything I know about Java and Netbeans tells me that I shouldn't have a problem as soon as I enter in a choice for the loop.
Solution
I eventually found that this configuration of the while loop makes the program work fine:
Scanner keyboard = new Scanner(System.in);
while (choice != 4) {
//First prompting of the menu
System.out.println(menu);
choice = keyboard.nextInt();
//Program does no work unless this is here.
ofileName = keyboard.nextLine();
I don't know why, exactly, but it does.
Answered By - RoverRush4
Answer Checked By - Robin (JavaFixing Admin)