Jump to content

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.");
  }
}

 

So I'm calling the main methods of other classes and I get this. Me rewriting the other programs isn't going to be easy as they both have dozens of inputs.

Spoiler

java.io.IOException: Cannot run program "walkTheCity.java": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at java.lang.Runtime.exec(Runtime.java:617)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at gabrielGameLauncher.main(gabrielGameLauncher.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

  import java.util.Scanner;
  import java.io.IOException;
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();
    if(gchoice==1||gchoice==2){
    // compiles code
      Process g1 = Runtime.getRuntime().exec("walkTheCity.java");
      Process g2 = Runtime.getRuntime().exec("AdventureGameS2.java");
      Process processRun = null;
      
      //the 2 code runners
      if(gchoice==1){
      processRun = null;
      processRun = Runtime.getRuntime().exec("walkTheCity");
      
      }
      if(gchoice==2){
      processRun = null;
      processRun = Runtime.getRuntime().exec("AdventureGameS2");

      }
    }
    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.");
  }
}

Thanks for any help

Good luck, Have fun, Build PC, and have a Wii and PS2 as your only consoles.

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Asrock RX9070xt Steel Legends, Corsair RM750X, 500gb 850 evo, 2tb rocket and 5tb Toshiba x300, 3x 6TB WD Black W10 all in a Obsidian 750D airflow.
GF PC: (NightHawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Sapphire RX 6700XT  Nitro+, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb 860 evo, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 35mm F1.4, Helios 44

Link to post
Share on other sites

1 minute ago, GDRRiley said:

So I'm calling the main methods of other classes and I get this. Me rewriting the other programs isn't going to be easy as they both have dozens of inputs.

  Hide contents

java.io.IOException: Cannot run program "walkTheCity.java": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at java.lang.Runtime.exec(Runtime.java:617)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at gabrielGameLauncher.main(gabrielGameLauncher.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)


  import java.util.Scanner;
  import java.io.IOException;
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();
    if(gchoice==1||gchoice==2){
    // compiles code
      Process g1 = Runtime.getRuntime().exec("walkTheCity.java");
      Process g2 = Runtime.getRuntime().exec("AdventureGameS2.java");
      Process processRun = null;
      
      //the 2 code runners
      if(gchoice==1){
      processRun = null;
      processRun = Runtime.getRuntime().exec("walkTheCity");
      
      }
      if(gchoice==2){
      processRun = null;
      processRun = Runtime.getRuntime().exec("AdventureGameS2");

      }
    }
    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.");
  }
}

Thanks for any help

This is kind of a trivial one, but did you ensure that the walkTheCity.java file is in the same directory as your GameLauncher file calling it? And did you ensure everything is spelled correctly? It's saying that it can't find the file, so I'd verify that it's there first.

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to post
Share on other sites

Just now, panther420 said:

This is kind of a trivial one, but did you ensure that the walkTheCity.java file is in the same directory as your GameLauncher file calling it? And did you ensure everything is spelled correctly? It's saying that it can't find the file, so I'd verify that it's there first.

yep there all in the same folder and all open in Dr Java.

Good luck, Have fun, Build PC, and have a Wii and PS2 as your only consoles.

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Asrock RX9070xt Steel Legends, Corsair RM750X, 500gb 850 evo, 2tb rocket and 5tb Toshiba x300, 3x 6TB WD Black W10 all in a Obsidian 750D airflow.
GF PC: (NightHawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Sapphire RX 6700XT  Nitro+, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb 860 evo, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 35mm F1.4, Helios 44

Link to post
Share on other sites

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.");
  }
}

 

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to post
Share on other sites

3 minutes ago, panther420 said:

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.

ok that worked. just had to remove String[] args from my main method in each of the programs. 

Good luck, Have fun, Build PC, and have a Wii and PS2 as your only consoles.

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Asrock RX9070xt Steel Legends, Corsair RM750X, 500gb 850 evo, 2tb rocket and 5tb Toshiba x300, 3x 6TB WD Black W10 all in a Obsidian 750D airflow.
GF PC: (NightHawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Sapphire RX 6700XT  Nitro+, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb 860 evo, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 35mm F1.4, Helios 44

Link to post
Share on other sites

Just now, GDRRiley said:

ok that worked. just had to remove String[] args from my main method in each of the programs. 

Cool. You should mark the post as solved and highlight a correct answer for future reference.

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to post
Share on other sites

Just now, panther420 said:

Cool. You should mark the post as solved and highlight a correct answer for future reference.

I did it isn't my first time on this forum if you couldn't tell from post or badges. 

Good luck, Have fun, Build PC, and have a Wii and PS2 as your only consoles.

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Asrock RX9070xt Steel Legends, Corsair RM750X, 500gb 850 evo, 2tb rocket and 5tb Toshiba x300, 3x 6TB WD Black W10 all in a Obsidian 750D airflow.
GF PC: (NightHawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Sapphire RX 6700XT  Nitro+, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb 860 evo, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 35mm F1.4, Helios 44

Link to post
Share on other sites

Just now, GDRRiley said:

I did it isn't my first time on this forum if you couldn't tell from post or badges. 

True. Sorry about that.

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×