Jump to content
  • Announcements

    • alpenwasser

      Please Use CODE Tags   31 Jan 2016

      Welcome to the Programming and Software Design Section,

      When asking for help with programming issues, please use the code tags to enclose your code, it makes things much more easily readable for the people trying to help you, thus improving your chances of actually getting help.
        To add code tags, click the <> button on the editor toolbar, then enter your code in the code editor that appears. If you are on a mobile device, or prefer to use BBCode, you can use [code] // Your code here // It will be syntax highlighted, though not necessarily corectly. [/code] (but the code editor is more consistent and less buggy).
GDRRiley

Java throwing IOexception when running Main Method from another Clas

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

 

Recommended Posts

Posted · Original PosterOP

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 last gen console for use once a year. I should answer most of the time between midday and midnight.

NightHawk: i5 6600k @4.2, ASUS Z170m-plus, H105, 16gb corsair vengeance LPX, Strix GTX 970 XFX RX 580 8GB, Corsair RM750X, 500 gb 850 evo, 250gb 750 evo and 5tb Toshiba x300

My compute server (remember to add link) HP DL380G6 2xE5520 24GB ram with 4x146gb 10k drives and 4x300gb 10K drives, running NOTHING can't get anything to work :)

WIP NAS Cisco Security Multiservices Platform server e5420 12gb ram, 1x6 1tb raid 6 for plex + Need funding 16+1 2tb raid 6 for mass storage.

PSU White list:

I run the boat topic

 

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
Posted · Original PosterOP
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 last gen console for use once a year. I should answer most of the time between midday and midnight.

NightHawk: i5 6600k @4.2, ASUS Z170m-plus, H105, 16gb corsair vengeance LPX, Strix GTX 970 XFX RX 580 8GB, Corsair RM750X, 500 gb 850 evo, 250gb 750 evo and 5tb Toshiba x300

My compute server (remember to add link) HP DL380G6 2xE5520 24GB ram with 4x146gb 10k drives and 4x300gb 10K drives, running NOTHING can't get anything to work :)

WIP NAS Cisco Security Multiservices Platform server e5420 12gb ram, 1x6 1tb raid 6 for plex + Need funding 16+1 2tb raid 6 for mass storage.

PSU White list:

I run the boat topic

 

Link to post
Share on other sites
Posted · Best Answer
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
Posted · Original PosterOP
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 last gen console for use once a year. I should answer most of the time between midday and midnight.

NightHawk: i5 6600k @4.2, ASUS Z170m-plus, H105, 16gb corsair vengeance LPX, Strix GTX 970 XFX RX 580 8GB, Corsair RM750X, 500 gb 850 evo, 250gb 750 evo and 5tb Toshiba x300

My compute server (remember to add link) HP DL380G6 2xE5520 24GB ram with 4x146gb 10k drives and 4x300gb 10K drives, running NOTHING can't get anything to work :)

WIP NAS Cisco Security Multiservices Platform server e5420 12gb ram, 1x6 1tb raid 6 for plex + Need funding 16+1 2tb raid 6 for mass storage.

PSU White list:

I run the boat topic

 

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
Posted · Original PosterOP
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 last gen console for use once a year. I should answer most of the time between midday and midnight.

NightHawk: i5 6600k @4.2, ASUS Z170m-plus, H105, 16gb corsair vengeance LPX, Strix GTX 970 XFX RX 580 8GB, Corsair RM750X, 500 gb 850 evo, 250gb 750 evo and 5tb Toshiba x300

My compute server (remember to add link) HP DL380G6 2xE5520 24GB ram with 4x146gb 10k drives and 4x300gb 10K drives, running NOTHING can't get anything to work :)

WIP NAS Cisco Security Multiservices Platform server e5420 12gb ram, 1x6 1tb raid 6 for plex + Need funding 16+1 2tb raid 6 for mass storage.

PSU White list:

I run the boat topic

 

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


  • Recently Browsing   0 members

    No registered users viewing this page.


×