Issue with executing batch file(in the User folder) using java - java

I am executing a batch file (placed in the User's folder) using below code :
Process p = Runtime.getRuntime().exec(System.getProperty("user.home") + "/myapp/bin/dashboard.bat");
OR
Process process = new ProcessBuilder(System.getProperty("user.home") +"/myapp/bin/dashboard.bat").start();
dashboard.bat run some jars which has UI interface contains :
#echo off
TITLE MyApp Console
if exist %APP_HOME% goto checkuserdata
set APP_HOME=C:\Users\<UserName>\myApp
:checkuserdata
if exist %APP_USERDATA_DIR% goto setuserdatadir
set APP_USERDATA_DIR_TMP=%APP_HOME%\userdata
goto startapp
:setuserdatadir
set APP_USERDATA_DIR_TMP=%APP_USERDATA_DIR%
:startapp
set POI_JARS= .. // Path of some jar file inside the APP_HOME
set JAVAX_MAIL_JARS= .. // Path of some jar file inside the APP_HOME
set C3P0_JARS= .. // Path of some jar file inside the APP_HOME
set APPLET_JARS= .. // Path of some jar file inside the APP_HOME
set APP_CLASS_PATH= .. // Path of some jar file inside the APP_HOME
java -version
java -Djsse.enableSNIExtension=false -Djava.util.logging.config.file=%APP_USERDATA_DIR_TMP%\config\log.properties -classpath %APP_EXT_CLASS_PATH%;%APP_CLASS_PATH% net.ui.Dashboard "%APP_HOME%" "%APP_USERDATA_DIR_TMP%"
pause
after executing the dashboard.bat in TaskManager process get started but we are not able to get UI interface (i think it run as background process - not sure).
But if we move the dashboard.bat to some other location [other than C drive(OS drive)] and then if we run the batch file from java then it works fine.
please suggest any other way to achieve this or tell me where I am doing wrong ?
Thanks in advance.

Related

On using a batch file in cmd Could not find or load main class for a program

The error image
Error: Could not find or load main class JDBCExample1.java
Caused by: java.lang.ClassNotFoundException:JDBCExample1.java
Batch file used:
set path=C:\Program Files\Java\jdk-10.0.2\bin;
set classpath=C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc14.jar;.
javac JDBCExample1.java
java JDBCExample1.java
pause
The PATH and the CLASSPATH have been set correctly in Environment variables too.
Also the directory used is correct.
Try giving
“java JDBCExample1”
Instead of
“java JDBCExample1.java”
In the last line. One does not use .java while running the program

Adding Runnable Thread class to JAR file using Terminal

When I try to run the command
jar uvf Century.jar Century$1.class
from the command line, I get the following response:
adding: Century.class(in = 6441) (out= 3544)(deflated 44%)
Notice that instead of adding Century$1.class, is adds Century.class. The issue here is that Century$1.class is the class file for a new runnable thread that gets created in Century.class, which is the main class. Is there some way I can add the Century$1.class file to the jar?
try any of
jar uvf Century.jar Century\$1.class
jar uvf Century.jar 'Century$1.class'
A unix shell will interpret $1 as the value of the first command line parameter which is not what you want, you want the $ verbatim.
And please remember to comment or accept an answer given to your last question Datepicker Multiple Dates not working in Google Apps Script

Hot to set the working directory of a process in Java

I am trying to set the working directory of a process that will be running an exe.
Here is what I have so far:
public Process launchClient() throws IOException {
File pathToExecutable = new File(currentAccount.getAbsoluteFile()+"/Release", "TuringBot.exe");
System.out.println(pathToExecutable);
ProcessBuilder builder = new ProcessBuilder(pathToExecutable.getAbsolutePath());
builder.directory( new File( currentAccount, "Release" )); // this is where you set the root folder for the executable to run with
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
return builder.start();
}
but every time I launch it, the exe attempts to run but fails to because the current directory is wrong and it can't properly access the required files.
EDIT:
Here's a little more about whats going on...
I have x amount of client folders, each contains an executable file that needs to be started and relies on the contents of its local folder to execute properly, however when running this code to launch each executable file the working path for each unique executable is not being applied and is rather the default directory (the JAR's locations).

Beanshell will not allow me to add jars to the "default" JRE classloader?

I have a question about Beanshell that I can't find an answer to anywhere. I am only able to run Beanshell scripts in 1 of 2 ways:
Where Classpath is defined before invoking Beanshell and Beanshell uses
the JRE default classloader.
Where no classpath is defined at all before starting Beanshell and then I use
addClassPath() and importCommands() to dynamically build the classpath
within Beanshell's classloader. This method does not seem to inherit a jars
that were part of the default JRE classloader.
After much experimentation, I have learned that I am unable to start a script with a pre-defined Classpath and then be able to add to the classpath by using addClassPath(). I don't know if this is as-designed or if I am doing something wrong?
It is very easy to see for yourself what my problem is. For example, here is the script:
::Test.bat (where bsh.jar exists in JRE/lib/ext directory)
#echo off
set JAVA_HOME=C:\JDK1.6.0_27
:: first invoke: this first command works
%JAVA_HOME%\jre\bin\java.exe bsh.Interpreter Test.bsh
:: second invoke: this command fails
%JAVA_HOME%\jre\bin\java.exe -cp ant.jar bsh.Interpreter Test.bsh
The second invoke causes this error:
Evaluation Error: Sourced file: Test.bsh : Command not
found: helloWorld() : at Line: 5 : in file: Test.bsh : helloWorld ( )
Test.bat launches this Beanshell script:
// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath("bin");
importCommands("bin");
helloWorld();
And, this is my helloWorld.bsh script:
// File: helloWorld.bsh
helloWorld() {
System.out.println("Hello World!");
}
Your Test.bsh has a slight error: importCommands looks for a directory called "bin" in the class path and loads all .bsh files from there, so what you should add to addClassPath is the current directory:
// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath("."); // current directory
importCommands("bin");
helloWorld();
The code you had works in the first case because the current directory is in the default system class path. The problem is that the -cp switch overrides the default class path, so importCommands no longer has any way to find the bin directory.
Alternatively you can add . to the classpath on the JVM level:
%JAVA_HOME%\jre\bin\java.exe -cp .;ant.jar bsh.Interpreter Test.bsh

exec, files location problem

In my jar application I do some calculations in exe program. When files and program.exe was in the same dir I used this command:
String[] str={"program.exe", "file1.txt", "file2.txt"};
pr = rt.exec(str);
and it worked great. But when I moved files to other dir and I try use this command:
String[] str={"program.exe", "temp\\file1.txt", "temp\\file2.txt"};
pr = rt.exec(str);
program.exe doesn't see files. What is more odd it start to see files when I change its names for anything else that default. file1.txt, file2.txt and temp are created in my jar program before program.exe start.
edit:
When problem started I try sth like this: default names file1.txt and file2.txt, I changed to aaa.txt and bbb.txt (in windows), and then:
String[] str={"program.exe", "temp\\aaa.txt", "temp\\bbb.txt"};
and it works.
edit2:
Now I know that problem is in program.exe. When I use it from command line (not from jar), like this:
program.exe temp\file1.txt temp\file2.txt
error:
FANN Error 1: Unable to open configuration file "temp\file1.txtÉ║#" for reading.
fann is artificial neural network library. When I copy files to program.exe dir:
program.exe file1.txt file2.txt
it works! When I changed files names in temp and do:
program.exe temp\file1aaa.txt temp\file2bbb.txt
it works also! So it is fann lib bug?
I'd use the ProcessBuilder api (it gives you much more control than Runtime.exec()) and I'd also use absolute paths:
File directory = new File("/path/tp/program.exe's/parent");
int returnCode = new ProcessBuilder("program.exe",
new File(directory, "temp/file1.txt").getAbsolutePath(),
new File(directory, "temp/file2.txt").getAbsolutePath()
)
.directory(directory).start().waitFor();
Give complete path of the filename and see. Something like below
String[] str = {"program.exe", "D:\\temp\\file1.txt", "D:\\temp\\file2.txt"};
If your OS is UNIX based then change it accordingly.
Have you tried relative path for finding the location
like
abc (folder)
-> code(folder)
-->Program.Java
-> temp
--> file1.txt
so
when u run ur program in Eclipse IDE
ur relative path will be from program.java file is
../temp/file1.txt
And try to use / instead of \ so that it wont take as an escape character.
when u run from a jar
You need to extract the temp folder from jar to outside
abc (folder)
-> jar (folder)
-->Program.jar
-> temp
--> file1.txt
OR
Read the jar content from the program as a zip file. Reach your temp folder inside it by code and then read the content as input stream.

Categories