Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Run cmd commands through java

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Run cmd commands through java

    I started this week with Java so I’m a beginner, not sure if my question is as well, so I post it here. I would like my application to execute a command in cmd. The command looks like
    start "" /D "C:\Riot Games\League of Legends\RADS\solutions\lol_game_client_sln\release s\0.0.1.55\deploy" "League of Legends.exe" "8394" "LoLLauncher.exe" "" "spectator 95.172.65.26:8088 P3hNrXYZlaM3iJ9ximtzJWHbwLhvbimJ 953089676 EUN1"
    So my question is how do I do this?

    I tried this, (just copy paste the command):
    package MyProject;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    public class Cmd {
     
    	public static void main(String[] args) {
    		try {
    			final Process process = Runtime.getRuntime().exec("start \"\" /D \"C:\\Riot Games\\League of Legends\\RADS\\solutions\\lol_game_client_sln\\releases\\0.0.1.55\\deploy\" \"League of Legends.exe\" \"8394\" \"LoLLauncher.exe\" \"\" \"spectator 95.172.65.26:8088 P3hNrXYZlaM3iJ9ximtzJWHbwLhvbimJ 953089676 EUN1\"");
    		    final InputStream in = process.getInputStream();
    		    int ch;
    		    while((ch = in.read()) != -1) {
    		    	System.out.print((char)ch);
    		    }
    		 } catch (IOException e) {
    			 e.printStackTrace();
    		 }
    	}
    }

    but than I get the error:

    java.io.IOException: Cannot run program "start": CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden
    	at java.lang.ProcessBuilder.start(Unknown Source)
    	at java.lang.Runtime.exec(Unknown Source)
    	at java.lang.Runtime.exec(Unknown Source)
    	at java.lang.Runtime.exec(Unknown Source)
    	at MyProject.Cmd.main(Cmd.java:12)
    Caused by: java.io.IOException: CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden
    	at java.lang.ProcessImpl.create(Native Method)
    	at java.lang.ProcessImpl.<init>(Unknown Source)
    	at java.lang.ProcessImpl.start(Unknown Source)
    	... 5 more


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Run cmd commands through java

    Have you tried the program with other DOS commands? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Run cmd commands through java

    When I change the command to: "notepad" it opened notepad as suspected.
    Last edited by java(); September 17th, 2014 at 03:42 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Run cmd commands through java

    Ok, that would mean that the java code does what is expected.
    Do a search on the start command to see how it works and if it can be used the way you want.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    java() (September 18th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Run cmd commands through java

    It is working, you pointed me in the right direcition. It was the start command. for googlers who land on this page. I used the following, not sure why this is working but it does.

    package MyProject;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    public class Cmd {
     
       public static void main(String[] args) {
          try {
             final Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start notepad"});
             final InputStream in = process.getInputStream();
             int ch;
             while((ch = in.read()) != -1) {
                System.out.print((char)ch);
             }
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
     
    }

Similar Threads

  1. java commands
    By sunilhs in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 21st, 2014, 10:01 AM
  2. cmd java run issue
    By 0w1 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 11th, 2013, 05:34 PM
  3. [SOLVED] How to referenc a class file. using only notepad and run cmd.
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 53
    Last Post: May 14th, 2012, 11:22 PM
  4. Replies: 7
    Last Post: October 13th, 2011, 03:03 AM
  5. [SOLVED] Turn Based Game run by Commands
    By EggoH1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 11th, 2011, 10:23 AM