0

I can't install Java on the computer, I only have a 'Java folder' on my desktop. Because of that executing 'cmd.exe' commands with Java doesn't work. Is there any method of pointing to the 'cmd.exe' and telling java to run a command using it?

I mean somethind similar to what I do in my 'start.bat' file i use to run .jar's - start C:\Users\kamilkime\Desktop\JEclipse\Java\jdk1.7.0_75\bin\java -jar Calculator.jar

I tried using this two methods, both don't work and throw the same exception:

//Method 1
Runtime.getRuntime().exec("cls");

//Method 2
ProcessBuilder builder = new ProcessBuilder("cls");
builder.start();

//Exception stacktrace
java.io.IOException: Cannot run program "cls": CreateProcess error=2, Nie można odnaleźć określonego pl
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 com.gmail.kamilkime.Main.clearConsole(Main.java:82)
at com.gmail.kamilkime.Main.start(Main.java:61)
at com.gmail.kamilkime.Main.main(Main.java:36)

Caused by: java.io.IOException: CreateProcess error=2, Nie można odnaleźć określonego pl
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 6 more

//edit 08.10.2015

Ok, so thanks to @9dan I've managed to run a console window, but... Now I can't do anything with it - cannot println() a message, cannot get a user input. Is it posibble to do sth like that (printing/getting input)?

BTW, the thing I'm trying to do the whole time is finding a way to enable the console program by double-clicking the .jar file, not by using 'java' command in 'cmd.exe'.

Now, I'm using this code and I'm stuck - I don't know what to do: http://pastebin.com/VLkr1RFd

Kamilkime
  • 238
  • 3
  • 7
  • Look for "Clear Console" on here. There are some similar questions. – Fildor Oct 07 '15 at 14:02
  • 'cls' is a DOS command (e.g. cmd.exe /c "cls") – 9dan Oct 07 '15 at 14:05
  • https://teamtreehouse.com/community/is-it-possible-to-clear-the-console-during-execution-in-a-java-workspace – Fildor Oct 07 '15 at 14:06
  • Possible duplicate of [Java: Clear the console](http://stackoverflow.com/questions/2979383/java-clear-the-console) – Fildor Oct 07 '15 at 14:11
  • First answer in marked dupe is the same as in that forum I linked. So there should be something to it. – Fildor Oct 07 '15 at 14:12
  • @Fildor All I've already found on stack uses first or the second method I've posted - both don't work for me. The second link you posted - I'm using Windows, so ASCII Escape codes won't work in the console – Kamilkime Oct 07 '15 at 14:14
  • @9dan I know, if I run it manually - it works as it it supposed to work, but if I run it using Java code - it doesn't work. I think it happens because I can't add Java to windows enviroment variables :/ – Kamilkime Oct 07 '15 at 14:14
  • @Kamilkime I mean 'cls' is not executable because it is a command of cmd.exe, should use something like exec("cmd.exe /c \"cls\"") – 9dan Oct 07 '15 at 14:24
  • @9dan I've edited the question, maybe you could help me once more :) – Kamilkime Oct 08 '15 at 18:52

0 Answers0