The command that is built is working fine on the command prompt but it not working in eclipse . This is how this looks :
String javaHome = System.getProperty("java.home");
cmds.add(javaHome+"/bin/java");
cmds.add("-cp");
cmds.add(_devInstallConfig.getProperty("CP"));
// CP = ("D:\Perforce\depot\nginst\src13920\nginst-install\repo-bootstrap-classpath-13.9.2.0-170213.1854.jar");
cmds.add("com.oracle.cie.repository.stager.CarbStager");
cmds.add("-repoURL "+repoURL);
cmds.add("-props");
cmds.add(_devInstallConfig.getProperty("PROPERTIES_FILE_LOCATION"));
//PROPERTIES_FILE_LOCATION=("C:\Users\bpurana.ORADEV\Desktop\carb.properties");
cmds.add("-repoBaseDir");
cmds.add(_ngInstallLocation.getParentFile().getAbsolutePath());
//repoBaseDir=("D:\Perforce\depot\nginst\src13920\nginst-install");
cmds.add("-PUBLISH_TYPE=BOTH");
cmds.add("-CARB_OUTPUT_DIR="+temp_file);
cmds.add("-NGINST_VERSION");
cmds.add(_devInstallConfig.getProperty("NGINST_VERSION"));
ProcessBuilder pb = new ProcessBuilder(cmds);
pb.directory(_ngInstallLocation.getParentFile());
Process process = pb.start();
The error that I am seeing is this :
Error: Could not find or load main class com.oracle.cie.repository.stager.CarbStager
Edit : I have updated the code snippet with the actual code ( and sharing sample examples of what each value means)
Related
I want to use ProcessBuilder to create an environment that I can reuse for multiple batch files, in order to automate some testing I repeatedly perform. Specifically, I need to run vcvars64.bat (x64 Native Tools Command Prompt for VS 2019) to set up the system environment, before I run my tests.
To manually run the tests, I would bring up a command prompt, run vcvars64.bat, and then manually run a number of batch files.
I have been able to use ProcessBuilder to launch my test batch files, but they return the error that I see if I forgot to run vcvars64.bat before running the tests.
My attempts so far have been to instantiate a ProcessBuilder using vcvars64.bat as the command, .start() it, .waitFor() the Process to finish, then reuse that same ProcessBuilder for Test1.bat then Test2.bat etc, in the hopes it would retain the environment settings.
Here is the relevant section of my code:
ProcessBuilder processBuilder = new ProcessBuilder();
Process process;
Map<String, String> envMap = processBuilder.environment();
for( Map.Entry<String, String> entry : envMap.entrySet() )
{
System.out.println( "1 - Key: \"" + entry.getKey() + "\", Value: \"" + entry.getValue() + "\"" );
}
try
{
process = processBuilder.command( "C:\\Windows\\system32\\cmd.exe", "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat" )
.directory( new File( "C:\\bat\\" ) )
.redirectInput( new File( "C:\\bat\\", "cr4.txt" ) )
.redirectOutput( new File( edgePath, "tempFile.txt" ) )
.start();
MAIN_LOGGER.log( Level.INFO, "Waiting for the CMD process to finish..." );
process.waitFor();
envMap = processBuilder.environment();
for( Map.Entry<String, String> entry : envMap.entrySet() )
{
System.out.println( "2 - Key: \"" + entry.getKey() + "\", Value: \"" + entry.getValue() + "\"" );
}
// Now try to run my batch file that requires parameters normally set by vcvars64.bat
process = processBuilder.command( "C:\\bat\\TestBatch.bat" )
.directory( new File( "C:\\bat\\" ) )
.redirectInput( new File( "C:\\bat\\", "cr4.txt" ) )
.redirectOutput( new File( "C:\\bat\\", "tempFile.txt" ) )
.start();
}
catch( IOException | InterruptedException e )
{
System.out.println( e.getLocalizedMessage() );
}
Is my plan correct, and my implementation buggy? Or do I need a new plan?
Unfortunately processBuilder.environment() won't pick up any changes inside the VC bat file so your double launch won't help. However depending on what vcvars64.bat looks like you may be able to package up your own launch.cmd file which you call passing two parameters: path to VC batch, and the path to your actual script to run. Then your ProcessBuilder command is just something like:
String launch = "C:\\bat\\launch.cmd";
String vcenv = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat";
String task = "C:\\bat\\TestBatch.bat";
String[] cmd = new String[] {"cmd.exe", "/c", launch, vcenv, task};
process = processBuilder.command(cmd);
Example launch.cmd:
call %1
call %2
exit /b %errorlevel%
Example task.bat:
echo RUNNING TASK %0
I would like to get current app path in the app.
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current config file path is: " + s);
It seems working well, BUT when I use like below.
> pwd
> /usr/local/
> java -jar app/getapppath.jar // executed app inside app folder
> Current config file path is : /usr/local // I want /usr/local/app
I can executed my app after 'cd app', but I want to fix it fundamentally. Any idea of this?
You can use classloader to get that information. Here is a example, which you can adopt to your needs:
package test;
import java.net.URL;
public class a {
public static void main(String[] args) {
URL u = ClassLoader.getSystemResource("test/a.class");
System.out.println(u);
}
}
In my case it works as:
$ cd /tmp
$ java -cp /home/me a
file:/home/me/a.class
$
I have a child java project which has groovy files added in classpath using eclipse. Parent java project triggers some functionality in child which uses Groovy library to run the scripts. So import works fine in eclipse environment with opened child project but if I run it from command line or if I close child project then I get groovy compilation error at import statement. How can I resolve this ? I want to avoid using evaluate() method.
Following is my master groovy:
package strides_business_script
abstract class Business_Script extends Script {
//some stuff
}
Following is the another groovy:
import static strides_business_script.StridesBusiness_Script.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
String Key = Part_Product_PartDetails
boolean containsData = checkIncomingMessage(Key)
if(containsData) {
def edgeKeyList = [PPR]
JSONArray partDetails = appendEdgeValueToMsg(edgeKeyList,Key,vertex,messageIterator);
//deleteMessages(Key);
JSONObject jsonObject = constructInfoWithPropertyJSON("NAME,PRODUCTTYPE,FGTYPE,UOM,ITEMCLASSIFICATIONBYMARKET");
jsonObject.put("PARTS",partDetails);
send(Product_AggPO_ProductDetails,convertJSONToString(jsonObject));
}
Edit:
My master script Business_Script.groovy resides in scripts/strides_business_script/ folder. All other scripts are in scripts/StridesComputationScripts/ folder and they import the Business_Script.groovy.
I run the application with remote debugging enabled like this:
java -cp "./lib/*:./scripts/strides_business_script/Business_Script.groovy" -Xdebug -Xrunjdwp:transport=dt_socket,address=6969,server=y -Dhibernate.cfg.xml.path=./conf/hibernate.cfg.xml -Dlog4j.configuration=file:./conf/log4j.properties com.biglabs.dataExtractor.dataDump.DataDumpDriver 7
and here I am trying to parse all computation scripts.
for (String scriptName : files) {
Script script = groovyShell.parse(new File(
SCRIPT_PLACED_AT + Constants.SLASH
+ SCRIPT_FILE_FOLDER + Constants.SLASH
+ scriptName));
scriptMping.put(scriptName, script);
}
It throws following exception while parsing using groovy shell:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/manoj/strides/release/strides/scripts/StridesComputationScripts/PRODUCT-script.groovy: 2: unable to resolve class strides_business_script.StridesBusiness_Script
# line 2, column 1.
import static strides_business_script.Business_Script.*;
^
/home/manoj/strides/release/strides/scripts/StridesComputationScripts/PRODUCT-script.groovy: 2: unable to resolve class strides_business_script.StridesBusiness_Script
# line 2, column 1.
import static strides_business_script.Business_Script.*;
^
2 errors
Fixed it by adding script path in comiler configuration:
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
String path = SCRIPT_PLACED_AT;
if(!SCRIPT_PLACED_AT.endsWith("/")){
path = path+ "/";
}
compilerConfiguration.setClasspath(path);
GroovyShell groovyShell = new GroovyShell(
compilerConfiguration);
for (String scriptName : files) {
Script script = groovyShell.parse(new File(
SCRIPT_PLACED_AT + Constants.SLASH
+ SCRIPT_FILE_FOLDER + Constants.SLASH
+ scriptName));
scriptMping.put(scriptName, script);
}
I'm using this Code Example, to execute commands in Tcl shell .
If you look at the main function down the page , the way of executing commands is :
SSHClient ssh = new SSHClient("linux_host", "root", "password");
List<String> cmdsToExecute = new ArrayList<String>();
cmdsToExecute.add("ls");
cmdsToExecute.add("pwd");
cmdsToExecute.add("mkdir testdir");
String outputLog = ssh.execute(cmdsToExecute);
In my program I'm doing :
SSHClient ssh = new SSHClient("linux_host", "root", "password");
List<String> cmdsToExecute = new ArrayList<String>();
cmdsToExecute.add("bpsh"); // open Tcl Shell
cmdsToExecute.add("set bps [bps::connect ... ]"); // Tcl shell commands
String outputLog = ssh.execute(cmdsToExecute);
Now the problem is that i can't execute commands from arrayList without exiting the Tcl Shell .
meaning if i run this Code :
SSHClient ssh = new SSHClient("linux_host", "root", "password");
List<String> cmdsToExecute = new ArrayList<String>();
cmdsToExecute.add("bpsh"); // open Tcl Shell
cmdsToExecute.add("set bps [bps::connect ... ]"); // Tcl shell commands
String outputLog = ssh.execute(cmdsToExecute);
cmdsToExecute.clear();
cmdsToExecute.add("set sf [$bps createSuperflow ... ]");
String outputLog = ssh.execute(cmdsToExecute);
i get that after the first execute on the remote machine it exited the first tcl shell and went back to original shell , and in the second execute it tries to run :
"set sf [$bps createSuperflow ... " in the original shell .
i assume because the line :
cmdsToExecute.add("bpsh");
doesn't exist .
The Code of the expect4j that i'm using is in the link above , can someone tell me what i need to modify so that i can execute many commands using ssh.execute() without it exiting the Tcl shell ?
You can try to build a file with the list of commands and source it.
Something like...
cmdsToExecute.add("echo \"\" > tmpcmd.txt");
cmdsToExecute.add("echo \"set bps [bps::connect ... ]\" >> tmpcmd.txt");
cmdsToExecute.add("echo \"set sf [$bps createSuperflow ... ]\" >> tmpcmd.txt");
cmdsToExecute.add("bpsh");
cmdsToExecute.add("source tmpcmd.txt");
I'm using graphviz to generate graphs based on the messages passed in a scala program.
To invoke the graphviz application from inside the scala program, I'm using the exec() method (similar to Java). It successfully executed the command and created the graph when I used the below code snippet:
var cmd: String = "dot -Tpng Graph.dot -o Graph.png"
var run: Runtime = Runtime.getRuntime() ;
var pr: Process = run.exec(cmd) ;
However It fails to execute after changing the path of the input and output files (I just included a directory inside which the input file and output file resides as shown below)
def main(args: Array[String]): Unit = {
var DirectoryName: String = "Logs"
var GraphFileName: String = DirectoryName + File.separator + "Graph.dot"
val GraphFileObj: File = new File(GraphFileName)
// var cmd: String = "dot -Tpng Graph.dot -o Graph.png"
var cmd: String = "dot -Tpng \"" + GraphFileObj.getAbsolutePath + "\" -o \"" + DirectoryName + File.separator + "Graph.png\"" ;
println(cmd)
var run: Runtime = Runtime.getRuntime() ;
var pr: Process = run.exec(cmd) ;
}
The same command when executed through terminal gives proper output. Can you please help me to find what I'm missing?
exec is not a shell...e.g. quoting won't work as you expect, and thus your path (which may contain spaces, etc) will not be processed as you expect. The command will be broken apart using StringTokenizer, and your literal quotes will be...well..literal.
Use the form of exec that takes an array instead, so you can tokenize the command correctly.
val args = Array[String]("dot", "-Tpng", GraphFileObj.getAbsolutePath, ...);
run.exec(args)