Why the commands written in a .bat running infinitely in java? - java

I have a .bat file whose job to find the version of the java. The command written inside the bat file is java -version (This is just an example, don't suggest alternative ways to get java version)
Code to run the .bat file:
String path = "cmd /c start d:\\java.bat";
Runtime rn = Runtime.getRuntime();
Process pr = rn.exec(path);
The bat file is running but running in a loop. But the expected behavior is it should open the command prompt and run the command only once

Because the name of the file is same as the command within it, the java.bat file is itself called from the java -version command. Just renaming the .bat file, or using java.exe -version would stop the issue.
Since extension for such executables and batch files is optional (*), the filename itself can be used alone as a command, read more about it here
(*) check great in-depth comment by #Compo
Also here is an example Java Bat =)

check java library instead of using a .bat file
Version version = java.lang.Runtime.version();
System.out.println("Java Version = "+version);
System.out.println("Java Version Feature Element = "+version.feature());
System.out.println("Java Version Interim Element = "+version.interim());
System.out.println("Java Patch Element Version = "+version.patch());
System.out.println("Java Update Element Version = "+version.update());
System.out.println("Java Version Build = "+version.build().get());
System.out.println("Java Pre-Release Info = "+version.pre().orElse("NA"));
here is source

I got the answer myself..
in stead of writing the commands simply in the bat file we can write in the below way and it worked for me
call java -version
Thanks,
Sudhansu

Related

Can I get the path of the currently running java executable?

Suppose I want to run a java program from the command line and I use this command:
myExes\java\java.exe AJavaProgram
As you can see, java.exe is not in my path, so I am running it manually rather than simply using the command java AJavaProgram.
I would like the program to return/print the first entry in the command, in this case, that entry is myExes\java. (Including java.exe at the end of this is also fine).
Is there a way to do this?
Initially, I thought it would be simple. args[0] would return the path, but that is not the case.
ProcessHandle.current() returns the current Java process. You can use that to see the full command in the process handle’s info:
ProcessHandle.current().info().command().ifPresent(
cmd -> System.out.println(cmd));
You can't get the string "myExes\java\java.exe", but you can get the location of the Java installation.
The following are results for running with OpenJDK 14 on Windows 10:
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("sun.boot.library.path"));
Output
C:\prog\Java64\jdk-14
C:\prog\Java64\jdk-14\bin
For reference, the full path of java.exe is:
C:\prog\Java64\jdk-14\bin\java.exe
When you do myExes\java\java.exe AJavaProgram AJavaProgram is the arg to java.exe and not the reverse. Its the same when you do java AJavaProgram, AJavaProgram is the arg to java.
How about this way?
You can get a java home dir.
String path = System.getProperty("java.home");

Java Runtime.getRuntime().exec() appears to be overwriting $PATH

For a project to automate some mutation adequacy testing, I'm trying to make GoLang from source from inside a Java program. I have been able to make it from source in the Terminal, and have tried using that command in Java's Runtime.getRuntime().exec() command:
String[] envp = new String[3];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
The equivalent command to this works fine in the Terminal. Running this code in java (And printing the output) gets the following:
./all.bash
##### Building Go bootstrap tool.
cmd/dist
go tool dist: FAILED: uname -r: exec: "uname": executable file not found in $PATH
So that's weird that it can't find uname. Again, if I enter 'uname' on the Terminal, it works fine. So I found the directory of uname ('which uname' gives '/usr/bin/uname') and set $PATH to that for this command:
String[] envp = new String[4];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
envp[3] = "PATH=/usr/bin";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
And that instead gets the output:
./all.bash
env: bash: No such file or directory
So when I set the path, it can't find the program in the directory. This suggests to me that when Runtime.getRuntime().exec() is called, it overwrites $PATH to be the directory I passed it, then overwrites the environment variables I gave it. But in order for ./all.bash to work, I need both paths to be in the $PATH variable. How can I do this?
On Mac OS X 10.11.6.
Runtime.exec was replaced by ProcessBuilder twelve years ago, as part of Java 1.5.
Among its many superior features is the ability to add to the existing environment:
ProcessBuilder builder = new ProcessBuilder("./all.bash");
builder.inheritIO();
builder.directory(
new File(System.getProperty("user.home") + "/Desktop/go/src"));
builder.environment().put("CC", "/usr/bin/clang");
builder.environment().put("GOROOT_BOOTSTRAP", "/usr/local/go");
builder.environment().put("CGO_ENABLED", "0");
builder.start();

Connecting to MySQL database using Java in command prompt

Since I don't have netbeans right now, I am trying to connect to MySQL database from my Java code through command prompt. But it it is not taking the mysql-coonectivity.jar file.
Does anyone know any way to run my program??? Please help.
Try executing the program as it is windows OS
java -cp .;path\of\your\mysql-connector-java-5.1.6.jar className
In case you don't have the mysql-connector-java-[version].jar get it from here
You need to add your MySQL connector jar file while compiling and running your program, you can do it the following way,
To compile :
javac -g -cp mysql-coonectivity.jar; YourFileName.java
To Run
java -cp mysql-coonectivity.jar; YourMainClass
NOTE: The above written syntax assumes that your jar file is present at same location as of your program.
To execute program in Linux (Ubuntu) OS follow the below command format:
java -cp .:/usr/share/java/mysql-connector-java-8.0.25.jar MainProgram
Note:
Assuming you are in the directory where MainProgram.class is exist
After .(dot -- this is for current directory) there is :(colon) as a separator in Linux

how to run shell script in java using Cygwin

From a long time i am struggling for with this program. I am having a shell script which accepts parameters as version number and path for the files. then that script creates Zip file with the name of version number congaing all file files to the folder.
I have installed Cygwin on following path D:/cygwin. I am coping required files to same location where cygwin is installed D:\cygwin\bin
Command
D:/cygwin/bin/bash -c '/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files
Or Can any one please suggest how to run shell script in java using Cygwin.
Rewriting the Problem:-
When i am trying to run following Command in command prompt it gives error
sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
Error:-C:\Documents and Settings\sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
/usr/bin/app.sh: line 51: lib/lib.sh: No such file or directory
But if i run the same command at
D:cygwin\bin\Test>sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
It works fine. Can any one suggest me how to avoid this kind of errors.
Runtime run = Runtime.getRuntime();
Process p = run.exec("D:/cygwin/bin/bash -c \'/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files");
p.waitFor();

Running a jar from shell script

I have a jar file named umar.jar in /root/umar/bin directory. I have a shell script file run.sh in same directory. Following is the content of run.sh
#!/bin/bash
"$JAVA_HOME"/bin/java -jar /root/umar/bin/umar.jar
Now when I run the shell script, I get the following error
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
After some googling, I found (as a folk from stackoverflow mentioned) that such errors occur when the Jar was compiled with a later version of the JDK than your JRE.
Now strange thing is, if I run this command directly on shell
java -jar umar.jar
it works perfectly fine. If the jar was compiled with a later version of JDK than my JRE, it shouldn't have had run at all even from the shell.
What do you suggest?
compare "java -version " and ""$JAVA_HOME"/bin/java -version"
You probably have multiple JVMs installed
As already mentioned - you're trying to use byte code compiled by the later compiler with old jvm.
Please note that if your PATH contains multiple java executables of different versions you can switch between them easily using '-version' key.
Suppose you have java5 and java6 at your PATH and java5 is located before java6 there. You can see that java5 is used by default then (if you execute 'java -version' it prints corresponding information). However, you can start java6 easily using command like 'java -version:1.6 ...' (e.g. if you execute 'java -version:1.6 -version' you see that java6 is used).
Export the java and jre home which you need to use in your sh file.
Run the jar using the exported java home
e.g
export JAVA_HOME=/opt/java6
export JRE_HOME=/opt/java6/jre
/opt/java6/bin/java -Xms2048m -Xmx2048m -jar abc.jar $1

Categories