This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I run a batch file from my Java Application?
Yes, I know this question has been asked many times. I have seen the solutions but non of them work for me. Currently I am doing this:
public static void main(String args[])
throws IOException
{
Runtime rt = Runtime.getRuntime();
rt.exec("C:\\sample-win32\\sample.bat");
}
Surprising part is eclipse run smoothly, no error or exception comes. But the file 'sample.bat' does not do what it is supposed to do.Please help.
Say this is my batch file. I am simply making a folder and opening it. When I double click the batch file it works fine. But when I use the java program, nothing happens.
md 1
start 1
You may have problems with paths in the batch file. Either use absolute paths only or add a cd command at the beginning of your script to jump to a defined folder.
You may expect output on the console but don't see any. This is because you have to redirect the stream. See this question and Brian's answer for a solution.
just keep in mind that passing something to the exec method is same as typing in run or in command
Related
This question already has answers here:
How do I run a Java program from the command line on Windows?
(13 answers)
Closed 2 years ago.
Disclaimer: The question has a similar solution and arguably duplicated from "How do I run a Java program from the command line on Windows?", but I still believe it can be helpful to those whom asked the same wrong questions like me.
My mistake, as it can be found in the comments, was to be expecting the compiler to spill out an extensionless executable file (like in C), but instead, I only saw a .class file being created, leading me to run mistakenly java filename.class
If that happens, just run java filename with no extension and the code will run correctly.
I'm a very beginner at java and don't have the option of using it in Linux, so I was following the tutorial from an algorithm course on Coursera from Princeton Uni, and I installed the jdk-11.0.2 from here: https://lift.cs.princeton.edu/java/windows/
Just to see myself getting a class file instead of an executable after running javac file.java
I searched around and couldn't find anything so far, so I hope someone here saw a similar issue at some point.
The code, just to avoid suspiciousness over it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
NB: IntelliJ on Windows compiles and runs it perfectly through the following command:
"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:{PATH_TO_INTELLIJ}\lib\idea_rt.jar=64104:{PATH_TO_INTELLIJ}\bin" -Dfile.encoding=UTF-8 -classpath {PATH_TO_MY_PROJECT};{PATH_TO_MY_PROJECT}\.lift\stdlib.jar;{PATH_TO_MY_PROJECT}\.lift\introcs.jar HelloWorld
A java compiler produces compiled code (.class) files.
To run it, you invoke it w/o the extension (i.e. java HelloWorld)
More details: https://beginnersbook.com/2013/05/first-java-program/
This question already has answers here:
How do I use Java to read from a file that is actively being written to?
(9 answers)
Closed 4 years ago.
I have a bash script in Linux (CentOS) that redirects output to a file. It takes a few minutes to run:
./myBashScript.sh >> file.csv
I have a java application that tries to read the file (only read) for further processing:
File file = new File("file.csv");
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
// do something...
}
These processes are on Crontab. It works all fine, but apparently whenever the bash script is redirecting the output to the file, the Java app can not read it. The logs shows "File not found" exception!
Is the file locked?! How can I fix the problem?
OK. Apparently I just need to somehow make sure the bash is finished, then run the Java app. Or once the whole bash is finished, just rename it! These trick should solve the problem
This question already has answers here:
Error: Could not find or load main class [duplicate]
(22 answers)
Closed 5 years ago.
I am trying to run these programs, but I am getting
Error: "Could not find or load main class"
Here's a screenshot of me trying to run the programs in cmd line:
Windows Powershell Screenshot:
This makes no sense to me seeing as how the files compiled just fine, which would imply that the main class was able to be found.
If anyone could explain what's going wrong I would appreciate it very much, thank you.
The UDPServer code:
The UDPClient code:
You have defined a package serverClient; at the top of both files.
So you should be having a directory named serverClient with your .class files.
if you wish to execute using java command line, you should execute from the src directory like this
PS ...\Programming Assignments\src > java serverClient.UDPServer
PS ...\Programming Assignments\src > java serverClient.UDPClient
It would be useful if you post your code here. But if I try a shot in the dark I would say that your filename doesn't match the classname in your Java file.
When running javac you pass in the paths to files you want to compile - hence those files are implicitly on the classpath. When running java you aren't passing in any files explicitly, so you have to include the current directory in the classpath in order for the JVM to know to look there.
$ javac Foo.java
$ java -cp . Foo
I use this Bash function pretty often for quick JVM/JDK experiments, if you want to try to replicate it in Powershell.
It's one of those cases where the current working directory probably should be on the classpath by default ~90% of the time, but it can't be in order to accommodate that last 10%. (Whether that's a good design decision or not is debatable, of course).
You are:
In the wrong directory. You should be in the directory that contains serverClient.
Using the wrong commands. They should be:
javac serverClient/*.java
java serverClient.UDPServer
java serverClient.UDPClient
This question already has answers here:
Java Runtime.getRuntime(): getting output from executing a command line program
(12 answers)
Closed 5 months ago.
Similar question
This question had an issue with filename vs classpath.
I am using classpath in my program and yet was not getting the output.
I searched a lot through various posts, but I was unable to resolve my error.
I have a class file in my D: The file just prints "hello world".
When I run it through command prompt as:
java -cp D:/ Test
it works fine.
But when I run the same inside a main method of another java program as:
Process p = Runtime.getRuntime().exec("java -cp D:/ Test");
I get no output, nor errors.
Can someone help me whats going wrong here?
You need to get InputStream out of the Process and read the input.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java execute a command with a space in the pathname
I am having following command
i_view32.exe C:\*.bmp /import_pal=C:\default.pal /convert=D:\temp\*.bmp
Which when i run through command prompt works fine.
I am trying to run the same command with the help of java.
Process p = Runtime.getRuntime().exec(System.getenv("ProgramFiles")+"\\IrfanView\\i_view32.exe c:\\*.bmp /import_pal= 1.pal /convert=d:\\temp\\*.bmp");
But i am not able to get Output in d:\\temp\\ Folder. Can any one suggest me where i am wrong.
Thanks in Advance..
Is there any other way to give "/" as i am using slash /import_pal=
2 your attempts are not exactly the same. I think that you executed command from command prompt when you were in c:\Program Files\IrfanView. When you are trying to run the same command from java you mention the full path. Since some programs are sensitive to current working directory I'd recommend you first to try to run the command from other directory (e.g. from c:) but specify full path.
If it works manually but does not work from java try to use ProcessBuilder instead of Runtime.exec(). Actually it is almost the same but it more Object Oriented and allows to specify working directory separately. I hope this will work for you.
If not try to play with quotes. Directory path 'c:\Program Files' contains space, so the path should be quoted.
Good luck.
Try to execute CMD
Example:
proc = Runtime.getRuntime().exec("cmd.exe /c dir");
It should work something like this, for your example it's a bit more complicated, but try it this way.