Java throwing IOexception when running Main Method from another Clas
Go to solution
Solved by panther420,
5 minutes ago, GDRRiley said:yep there all in the same folder and all open in Dr Java.
Then it seems like the better way to load the main method from another class would be to call it directly. According to the java docs the exec() method you're calling is for console commands, so perhaps you would use "java walkTheCity.java" instead of just the file name. But assuming both files are in the same package you should just be able to call walkTheCity.main() from gabrielGameLauncher. See here: https://stackoverflow.com/questions/13174031/how-to-run-java-class-file-from-another-class-file-java-newb.
EDIT: the code would probably look like this:
public class gabrielGameLauncher{ public static void main(String[] args) throws IOException{ Scanner scan= new Scanner(System.in); System.out.println("Enter 1 to play walk in the City.\nEnter 2 to play a cave adventure game.\nEnter 0 to stop playing."); int gchoice=1; while(gchoice!=0){ gchoice=scan.nextInt(); // run code based on decision if(gchoice==1){ walkTheCity.main(); // pass any args necessary here } if(gchoice==2){ AdventureGameS2.main(); } if(gchoice!=0){ System.out.println("Enter another code to play a game."); } } System.out.println("Thanks for playing hope you had a great time."); } }

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now