How to clear console in java (Using netbeans) - java

I am using jdj 1.7,netbeans 7 versions. I wrote a program that's working fine. Now i want to clear the console window after many outputs in the console. I tried the below line Runtime.getRuntime().exec("cls"); also tried with exec("clear"). But i am getting these errors:
Exception in thread "main" java.io.IOException: Cannot run program "cls": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at Periodical.main(Book.java:88)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
What's the problem here. How can i clear the console? Thank you

If you want to keep it portable you might want to use a library like jcurses. That will save you the trouble of having to detect what kind of console/terminal you are on and will give you a lot of other nice features like cursor and color control.

Related

Executing Windows shell commands using 'not-installed' Java

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

How do I use mysqlimport?

Here is the command I am running using Runtime.getRuntime().exec() in Java:
mysqlimport --fields-terminated-by=, --lines-terminated-by="|" --local
--user=u --password=p DatabaseName
txtpath
Here is the error I get:
java.io.IOException: Cannot run program "mysqlimport": CreateProcess error=2, The system cannot find the file specified
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 databaseCommunication.UploadThread.run(UploadThread.java:66)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
It seems like the problem should be that mysqlimport.exe is not installed or is not installed in the correct place, but I have tried downloading the mySQL utilities from http://dev.mysql.com/downloads/windows/installer/5.6.html and from https://dev.mysql.com/downloads/utilities/.
In order to make sure the problem was not that it could not find the file at "txtpath," I typed the full path into the command prompt, and the correct file was opened, so the error is definitely referring to mysqlimport.exe.
Googling my problem, the only threads I've been able to find refer to something called "Sqoop" which I am not familiar with, and they usually recommend downloading the mysql utilities.
For more context, I have been using BCP to upload data from a txt file to a sql server database, but now I need to do the same thing with mysql. If there is any way to use BCP (I'm pretty sure there isn't) or something else to bulk upload data from a local file I would be open to hearing that as well.
EDIT:
I am using Windows 8 on a remote desktop. I have manually added mysqlimport.exe to the PATH environment variable and it still gives the same error.
If you are on Windows, then try putting mysqlimport.exe as the command rather than just mysqlimport. If that still does not solve your problem, make sure that 'mysqlimport.exe' is in your PATH environment variable.

Error under the following program to write R Script separately

Calling R in java-Rcaller
After following above link I've created new "myScript.R" seperately.
Please tell me that it is showing "save workspace" for saving. I've saved it as file with .R extension. Please correct me if I'm wrong.
I'm recieving the following error on running this as same Java program (provided in above link) created in Netbeans IDE for connecting RCaller with Java:
run:
java.io.IOException: Cannot run program "Rscript": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at javaapplicationrcaller2.JavaApplicationRCaller2.<init>(JavaApplicationRCaller2.java:41)
at javaapplicationrcaller2.JavaApplicationRCaller2.main(JavaApplicationRCaller2.java:25)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 5 more
BUILD SUCCESSFUL (total time: 0 seconds)

How to run setup.exe file using java

I am developing an java application from which I have to run xyz_setup.exe installer. I tried the following code
String command = "C:\\xyz_setup.exe";
Runtime.getRuntime().exec(command);`
But it was throwing the following error
java.io.IOException: Cannot run program "C:\Users\NewtonApples\Downloads\idman614.exe": CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at upendra.OpenExternalApplication.main(OpenExternalApplication.java:19)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 4 more
Can any one suggest me how to do this?
Java (or likely any other process which uses CreateProcess system call directly) is not good with executables requiring access elevation.
You can get around that by executing your program via shell:
String command = "C:\\setup.exe";
Runtime.getRuntime().exec("cmd /c "+command);

Java - HtmlUnit - Unable to save HTML to file (in some cases)

I am having intermittent issues saving the response HTML in HtmlUnit.
Caused by: java.io.IOException: Unable to save file:C:\ccview\PP50773_4.0_walter\TSC_hca\Applications\HCA_J2EE\HCA\target\HtmlUnitTests\single\1\com\pnc\tsc\hca\ui\test\SiteCrawler\crawlSiteAsProvider\10.SiteCrawler.crawl.html
at com.pnc.tsc.hca.ui.util.GetUtil.save(GetUtil.java:128)
at com.pnc.tsc.hca.ui.util.GetUtil.add(GetUtil.java:75)
at com.pnc.tsc.hca.ui.util.GetUtil.click(GetUtil.java:49)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:87)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:61)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:63)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:63)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:63)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawl(SiteCrawler.java:54)
at com.pnc.tsc.hca.ui.test.SiteCrawler.crawlSiteAsProvider(SiteCrawler.java:50)
... 15 more
Caused by: java.lang.RuntimeException: java.io.IOException: The system cannot find the path specified
at com.gargoylesoftware.htmlunit.html.XmlSerializer.getAttributesFor(XmlSerializer.java:165)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printOpeningTag(XmlSerializer.java:126)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:83)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:93)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:93)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.asXml(XmlSerializer.java:73)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.save(XmlSerializer.java:55)
at com.gargoylesoftware.htmlunit.html.HtmlPage.save(HtmlPage.java:2259)
at com.pnc.tsc.hca.ui.util.GetUtil.save(GetUtil.java:126)
... 24 more
Caused by: java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.createFile(XmlSerializer.java:216)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.getAttributesFor(XmlSerializer.java:160)
... 32 more
Now, the parent directory exists and some other files have already been written to the directory. Looking at the filename, I don't see anything that would stand out as a red flag indicating the filename is bad.
What can I do to correct this error?
Thanks,
Walter
Well, you are well within the maximum path length. The exception trace clearly believes it is a path problem.
I would try some additional tests to help isolate the problem. Check the directory before writing with: File.exists(), File.isDirectory(), File.isWritable(). Similar checks on the file itself. May want to see if the disk is full too.
If you can get a little more information on the source of the problem, we can get it resolved.

Categories