Error Could not find or load main class -jar - java

I am working on a java UNO project, OS : Ubuntu 14. I am calling exec via passing command to run via a jar file with some set of sub commands of that jar file.
String finalOutputMSG = "";
String[] cmd = {JAVA_LOCATION, " -jar ", JAR_LOCATION, " " + inputFile, " -dir ", ".isc", " -out xml"};//java location provides java location, jar location provides jar location, inputfile contains input file's location -dir provides output directory with name .isc, -out is output file with file format for output is xml
Similar command ran properly without showing any errors but in a case where I am trying to import a file and convert it into another format eg .xlsx to .xml, is giving error. In commands it worked, I have already generated outputs from an input file.
finalOutputMSG = exec(cmd);
/**
* exec() is executed and outputs are displayed
*
* #param String[] command passed to jar
* #return output message containing outputs or output message
*/
private static String exec(String[] cmd) {
String outputMSG = "";
Process proc = null;
try {
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);//any error output generated by subprocesses merged with the standard output,
//read using the Process.getInputStream()
///* Start the process */
proc = pb.start();
if (debug) {
System.out.println("Process started !");
}
outputMSG = getOutput(proc);
if (debug) {
System.out.println("outputMSG " + outputMSG);
}
} catch (IOException e) {
if (debug) {
System.out.println("Exception in exec " + e.getMessage());
JOptionPane.showMessageDialog(null, "Exception in exec ");
}
// StringBuilder append = appendToFile.append("Exception in exec ").append(e.getMessage());
} catch (Exception e) {
if (debug) {
System.out.println("Exception in exec " + e.getMessage());
JOptionPane.showMessageDialog(null, "Exception in exec ");
}
} finally {
///* Clean-up */
proc.destroy();
if (debug) {
System.out.println("Process ended !");
}
}
return outputMSG;
}
/**
* Reads output from current process
*
* #param current process
* #return output read in current process
*/
private static String getOutput(Process p) {
StringBuilder outStream = new StringBuilder();
if (debug) {
System.out.println("StringBuilder initialized in getOutput");
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
if (debug) {
System.out.println("BufferedReader initialized in getOutput");
}
String line = null;
if (debug) {
System.out.println("in.readLine() in getOutput abt to be read");
}
while ((line = in.readLine()) != null) {
outStream.append(line);
if (debug) {
System.out.println("line in getOutput " + line);
System.out.println("outStream in getOutput " + outStream);
}
outStream.append("\n");
}
} catch (IOException e) {
if (debug1) {
System.out.println("IOException in getOutputs " + e.getMessage());
}
} catch (Exception ex) {
if (debug1) {
System.out.println("Exception in getOutputs" + ex.getMessage());
}
}
return outStream.toString();
}
Error Message depicted by Netbeans
Error: Could not find or load main class -jar
I have searched on the issue, but could not find any help that is useful, I could not understand, what is missing.

Solution:
String[] cmd = {JAVA_LOCATION, " -jar ", JAR_LOCATION, " " + inputFile, " -dir ", ".isc", " -out xml"};
I replaced the values that printed in console, and ran the command so got on terminal, it worked fine.
Solution: the command to be used must be without any spaces in the ends. Because terminal in linux interprets the commands like for "ls", but in java/ any programming language, it doesn't interprets for ls, so in case of the following parameter cmdarray
public Process exec(String[] cmdarray)
throws IOException
takes the command as it is.
String[] cmd = {JAVA_LOCATION, "-jar", JAR_LOCATION, inputFile, "-dir", ".isc", "-out", "xml"};

Related

imagemagick Convert working in Command line but not in java process runtime

I have a tif image where I am trying to draw a box and compress the image with LZW compression simultaneously.
this is the command I am running, and it works fine from windows command line.
C:\ImageMagick-7.1.0\convert.exe "C:\Users\admin\Orig.tif" -draw "rectangle 576,1069,943,1114" -compress LZW "C:\Users\admin\DrawLZW.tif"
when I try to execute the same command with my java program, I get an image file created, but the file size is 1kb
String[] cmd = {"C:\\ImageMagick-7.1.0\\convert.exe", "\"C:\\Users\\chris.macwilliams\\Orig.tif\"", "-draw", "\"rectangle 576,1069,943,1114\"", "–compress","LZW", "\"C:\\Users\\chris.macwilliams\\DrawLZWwithJava.tif\""};
LOGGER.info(cmd);
Process pt = Runtime.getRuntime().exec(cmd);
pt.waitFor();
if (pt.exitValue() != 0) {
LOGGER.error("ERROR with Image Magic Command exit value:" + pt.exitValue()+ " "+ commandTIF);
Any Ideas here?
Using IM Version: ImageMagick-7.1.0
I have included the test images that I am getting the errors on.
zip file download
If you are using an array, it's easier to declare the size of the array and add each argument before executing the cmd.
private void redactCMDArray() {
String[] cmd = new String[7];
cmd[0] = "C:\\ImageMagick-7.1.0\\convert.exe";
cmd[1] = "\"C:\\Users\\Administrator\\Desktop\\images\\Orig.tif\"";
cmd[2] = "-draw";
cmd[3] = "rectangle 576,1069,943,1114";
cmd[4] = "-compress";
cmd[5] = "LZW";
cmd[6] = "\"C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_CMD_Option1.tif\"";
System.out.println(Arrays.toString(cmd));
Process pt;
try {
pt = Runtime.getRuntime().exec(cmd);
pt.waitFor();
if (pt.exitValue() != 0) System.out.println("ERROR with Image Magic Command exit value:" + pt.exitValue()+ " " + Arrays.toString(cmd));
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
Another option, as stated by Esther, is to add a whitespace between parameters within the command and not pass an array.
private void redactCMDLine(){
String imPath = "C:\\ImageMagick-7.1.0\\convert.exe";
String imEXE = "/convert.exe";
String cmd = imPath + imEXE + " " + "C:\\Users\\Administrator\\Desktop\\images\\Orig.tif" + " " + "-draw \"rectangle 576,1069,943,1114\"" + " " + "-compress LZW" + " " + "C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_CMD_Option2.tif";
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
System.out.println("Exit code: " + p.exitValue());
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
If the IM4Java jar is available, a more straightforward solution would be the following.
private void redactIM4Java() {
ConvertCmd convertCmd = new ConvertCmd();
IMOperation op = new IMOperation();
op.addImage("C:\\Users\\Administrator\\Desktop\\images\\Orig.tif");
op.fill("Black");
op.draw("rectangle 576,1069,943,1114");
op.compress("LZW");
op.format("TIF");
op.addImage("C:\\Users\\Administrator\\Desktop\\images\\DrawLZW_MB_JavaIM4Java.tif");
try {
convertCmd.run(op);
} catch (IOException | InterruptedException | IM4JavaException e) {
e.printStackTrace();
}
}

Running multiple Batch files from different threads Java

I have n number of thread getting created at run time according to the input source files present in a folder. For every thread, I have one common class which has all the functions present that are used by every thread. Every thing is working perfectly except the part where batch files are run.
I have main class which is creating thread(which is working perfectly fine). Then I am creating batch files with relevant contents( which is also running perfectly). After that, only 1(can be anyone, no specific pattern) thread is able to execute the batch file and not the others.
Code:
String batch_content = "echo off \n "
+ "powershell.exe -file "
+ utility_path + "convertCSVSwiss.ps1 " + fpath + filename + " -executionpolicy Unrestricted \n ";
String batch_name = "batch_" + fname +"_"+sdf.format(cal.getTime())+ ".bat";
Utils.createBatchFile(batch_content, bat_file_path, batch_name);
Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);
Utils.createBatchFile is working fine which create a batch file with the batch content. But Utils.RunBatch seems to having some problem. Here is the code for RunBatch:
public static void RunBatch(String filepath, String filename,String csv_file_path,String fname) throws Exception {
try {
System.out.println("Started Program");
new File(csv_file_path + "\\" + fname).mkdir();
String filePath1 = filepath + filename;
System.out.println("Batch file running is " + filePath1);
Process p = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", filePath1 });
p.getOutputStream().close();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
My log file prints this:
Batch file running is C:\ER\ETL\bat files\batch_Sample_Data_10_40_16_12_40_37.bat
Batch file running is C:\ER\ETL\bat files\batch_ssd_10_40_16_12_40_37.bat
but it runs only the first one.
Any help would be appreciated.
P.S I am sorry if I missed any information that may be necessary to get this problem resolved. Please let me know and I can then edit my post.
EDIT:
Here is my code.
//main class to start new thread for every excel file present in the source directory
public class LoadData{
public static void main(String[] args) throws Exception{
try{
File folder = new File(fpath);
File[] listoffiles = folder.listFiles();
for (int i = 0; i < listoffiles.length; i++) {
if (listoffiles[i].isFile()) {
filename = listoffiles[i].getName();
c = filename.lastIndexOf(".");
absfilename = filename.substring(0, c);
System.out.println("File name with extension is "+filename);
System.out.println("File name is "+absfilename);
System.out.println("Starting thread for "+absfilename);
ConvertToCSV et = new ConvertToCSV();
et.fpath = fpath;
et.utility_path=utility_path;
et.filename=filename;
et.fname = absfilename;
et.bat_file_path =bat_file_path;
et.tpath =tpath;
et.csv_file_path=csv_file_path;
Thread t = new Thread(et);
t.start();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//class to create the batch file content
public class ConvertToCSV implements Runnable{
String fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path;
try{
String batch_content = "echo off \n "
+ "powershell.exe -file "
+ path_to_powershell_script_to_convert_excel_into_csv + "convertCSVSwiss.ps1 " + path_and_name_to_the_excel_file " -executionpolicy Unrestricted \n ";
String batch_name = "batch_" + excel_file_name +"_"+sdf.format(cal.getTime())+ ".bat";
Utils.createBatchFile(batch_content, bat_file_path, batch_name);
Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);
}
catch (Exception e) {
e.printStackTrace();
}
}
public class Utils{
//function to create the batch file
public static void createBatchFile(String batch_content, String path, String batch_name) throws IOException {
String p = path + batch_name;
File batfile = new File(p);
FileWriter fw = new FileWriter(batfile);
fw.write(batch_content);
fw.close();
}
//function to run the batch file
public static void RunBatch(String filepath, String filename,String csv_file_path,String fname) throws Exception {
try {
System.out.println("Started Program");
new File(csv_file_path + "\\" + fname).mkdir();
String filePath1 = filepath + filename;
System.out.println("Batch file running is " + filePath1);
Process p = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", filePath1 });
p.getOutputStream().close();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
EDIT2: I have added the run for ConvertTO CSV. My code is doing say 10 things, and 9 of them are working fine except running two batch files with different names from the same folder
public class ConvertToCSV implements Runnable{
String fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path,pg_db_url,pg_db,pg_db_uid,pg_db_pwd,plpgsql_path,Log_Path;
SimpleDateFormat sdf = new SimpleDateFormat("dd_mm_yy_hh_mm_ss");
Calendar cal = Calendar.getInstance();
#Override
public void run() {
try {
runConvertToCSV(fpath,utility_path,filename,fname,bat_file_path,tpath,csv_file_path,plpgsql_path);
} catch (Exception e) {
e.printStackTrace();
}
}
private void runConvertToCSV(String fpath,String utility_path,String filename,String fname,String bat_file,String tpath,String csv_file_path,String plpgsql_path) throws Exception{try{
String batch_content = "echo off \n "
+ "powershell.exe -file "
+ path_to_powershell_script_to_convert_excel_into_csv + "convertCSVSwiss.ps1 " + path_and_name_to_the_excel_file " -executionpolicy Unrestricted \n ";
String batch_name = "batch_" + excel_file_name +"_"+sdf.format(cal.getTime())+ ".bat";
Utils.createBatchFile(batch_content, bat_file_path, batch_name);
Utils.RunBatch(bat_file_path, batch_name,csv_file_path,fname);
}
catch (Exception e) {
e.printStackTrace();
}
}
EDIT3#:
My guess was that maybe because all the batch files are trying to access the same powershell script, that is why it is not working. But then i created ps script for every batch file. Also, added error stream to the stdout to check if there is any error and this is what i am getting:
Standard Error:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\ER\ETL\ETL_SOURCE\convertCSVSwiss_Swiss_Sample_Data.ps1:24 char:2
+ $Worksheet.SaveAs($ExtractedFileName,6)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
there are number of same error at different line. NOTE: It is the same ps script for all the batch files, it runs only for one and not for others. and that one can be anyone(no pattern).
If i run the above batch file manually, then it succeeds.

How to change path while running java file?

I am trying to run a java file through another java program . this is my code:
private static void printLines(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(
new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}
private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}
public static void main(String[] args) {
String[] credentials=new String[4];int k=0;
for (String s: args) {
System.out.println(s);
credentials[k]=s;k++;
if(k==4)
break;
}
try {
//runProcess("javac test2.java");
runProcess("java test2 "+credentials[0]+" "+credentials[1]+" "+credentials[2]+" "+credentials[3]+" ");
} catch (Exception e) {
e.printStackTrace();
}System.out.println("hI");
}
The problem is I have kept both the files(which I execute and the one which is executed by that file) in same folder but when I run this file it displays class not found error.. for test2.java and it probably due to the fact that it searches the class file test2.class in some other folder . what should I do?
my file structure:
x/y/Laj.java
x/y/test2.java
and it seaches the class file in x folder?
Use
Runtime.getRuntime().exec(command, null, workingDir);
where workingDir is :
workingDir- the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
If you run the first program using
java x.y.Laj
then you should change the line where you compose the command:
runProcess("java x.y.test2 "+credentials[0]+...
** Later **
Since the x.y is just a red herring, try setting the system property:
runProcess("java -Djava.class.path=\"/.../x/y\" " + credentials[0]+...
For production (start of Laj not from an IDE) consider setting CLASSPATH so that all class files can be found via the class path.

Java Powershell CreateProcess error=2, The system cannot find the file specified

I am executing powershell commands in java and I have written two programs, however the strange part is one works fine and the other throws the error. The code that throws the error is as shown
I have tried the following
1) Spcifying the fully specified path of powershell
2) My path variable has the following - "C:\WINDOWS\system32\WindowsPowerShell\v1.0"
I know I might be doing something trivial but its been a day and I am unable to figure out what the issue might be
import java.io.IOException;
public class FileCount {
public static void main(String[] args) {
Process flCntProcess = null;
try {
String test = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe -Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object }\"";
System.out.println("Powershell command : " + test);
ProcessBuilder builder = new ProcessBuilder(test);
builder.redirectErrorStream(true);
flCntProcess = builder.start();
// FILE COUNT OUTPUT STREAM PROCESSING
NotifyThreadComplete outputThread = new ProcessHandler(flCntProcess.getInputStream(),"OUTPUT");
outputThread.addListener(new ThreadCompleteListener() {
#Override
public void notifyCompletion(Thread t, long startTm, boolean didErrorOut, String noOfLines) {
System.out.println("Completed Output Stream Processing");
System.out.println("Printing values");
System.out.println("No of Lines : " + noOfLines);
System.out.println("Did Error out : " + didErrorOut);
if(didErrorOut) {
System.out.println("Do not continue with processing");
} else {
System.out.println("Continue with processing");
}
}
});
System.out.println("Starting output thread ");
outputThread.start();
} catch (Exception e) {
System.err.println("Exception while counting files using Powershell Command" + e.getMessage());
} finally {
if(flCntProcess != null && flCntProcess.getOutputStream() != null) {
try {
flCntProcess.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Error code indicates the file to execute can't be found. Try splitting up the program from its arguments:
String ps = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe";
String args = "-Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object}\"";
ProcessBuilder builder = new ProcessBuilder(ps, args);
The constructor of ProcessBuilder does not accept a single String containing a cli invocation, but an array of Strings containing in order :
the program to be executed
its arguments
See the javadoc
So it interprets your whole String test as the program name, splitting it up should work :
final String psh = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
final String args = "-Command & { Get-ChildItem C:\\temp -Recurse -force | Measure-Object }";
final ProcessBuilder builder = new ProcessBuilder(psh, args);

How to use Java Runtime.exec() with Windows REG utility to Read/Update/Delete entries in HKEY_LOCAL_MACHINE\...\CurrentVersion\Run?

I want to use Runtime.exec() to update the registry for HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
using the Windows REG command utility.
Need to be able to add/remove/read an entry from the "Run" key to allow my Swing application to run at startup and check if it is configured to run at startup so I can mark the option as checked or unchecked in the GUI. I had this working with JNI but the library was 32bit only so it doesn't work on 64bit. I'm thinking this will be a better approach. Don't even need to include a library this way and I don't think REG is going away or changing.
Has anyone done this before or know how to do this?
Thanks!
This approach might not work as expected. See in x64 Windows is there a way to run a Runtime.exec() process avoiding 'Registry redirection'
I added a couple new methods (addValue/deleteValue) to the example found here:
read/write to Windows Registry using Java
/**
* #author Oleg Ryaboy, based on work by Miguel Enriquez
*/
public class WindowsReqistry
{
/**
*
* #param location
* path in the registry
* #param key
* registry key
* #return registry value or null if not found
*/
public static final String readRegistry(String location, String key)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query \"" + location + "\" /v \"" + key + "\"");
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Output has the following format:
// \n<Version information>\n\n<key>\t<registry type>\t<value>
if (!output.contains("\t"))
{
return null;
}
// Parse out the value
String[] parsed = output.split("\t");
if(parsed.length > 0)
{
String result = parsed[parsed.length - 1].trim();
result = result.substring(1, result.length() - 1);
return result;
}
}
catch (Exception e)
{
}
return null;
}
static class StreamReader extends Thread
{
private InputStream is;
private StringWriter sw = new StringWriter();;
public StreamReader(InputStream is)
{
this.is = is;
}
public void run()
{
try
{
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e)
{
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public String getResult()
{
return sw.toString();
}
}
public static boolean deleteValue(String key, String valueName)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg delete \"" + key + "\" /v \"" + valueName + "\" /f");
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Output has the following format:
// \n<Version information>\n\n<key>\t<registry type>\t<value>
return output.contains("The operation completed successfully");
}
catch (Exception e)
{
}
return false;
}
public static boolean addValue(String key, String valName, String val)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec(
"reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Output has the following format:
// \n<Version information>\n\n<key>\t<registry type>\t<value>
return output.contains("The operation completed successfully");
}
catch (Exception e)
{
}
return false;
}
}

Categories

Resources