Java utility unable to find and read its properties file - classpath

I have to execute a java utility (UTL.jar) through a shell script (mainUTL.sh). It utilizes a properties file (SetupUTL.properties).
The location of the files is as following -
/a/b/mainUTL.sh
/a/b/UTL.jar
/a/b/SetupUTL.properties
I have setup my CLASSPATH as following in the mainUTL.sh -
export CLASSPATH=$CLASSPATH:/a/b/SetupUTL.properties:/a/b/UTL.jar
I am executing the script from command prompt (Login shell is csh) as following -
sh mainUTL.sh <$ARG1> &
and the script mainUTL.sh has the following java execution command -
nohup $JAVA_HOME/bin/java -d64 UTL.jar <$ARG1>
Now when I am executing the shell script from location /a/b/ as following
/a/b$: sh mainUTL.sh <$ARG1> &
The utility is executing fine.
But when I am executing the shell script from any other location say /a/ as following
/a$: sh mainUTL.sh <$ARG1> &
The utility is unable to find its properties file. The utility has following piece of code -
private boolean initPropertiesAndLog()
{
String str1 = null;
String str2 = null;
InputStream localInputStream = ClassLoader.getSystemResourceAsStream("SetupUTL.properties");
if (localInputStream == null) {
System.out.println("Unable to read SetupUTL.properties.properties file.");
return false;
}
I am not at all aware of Java codes, so please tell me what wrong I am doing as to not able to execute the utility fine.

You should not use ClassLoader.getSystemResourceAsStream
Try this
this.getClass().getClassLoader().getResourceAsStream("your_prop_file")
if fails, try to add a "/":
this.getClass().getClassLoader().getResourceAsStream("/your_prop_file")

Related

Getting a "Missing token: ASSIGN" error when running OpenModelica script through a batch file

I am trying to run an OpenModelica script using a DOS .bat file. But facing some issues.
The batch file runmodelica.bat has
%OPENMODELICAHOME%\bin\omc testscript.mos
The file testscript.mos is
loadModel(Modelica)
getErrorString()
loadFile("HelloWorld.mo")
simulate(HelloWorld)
getErrorString()
If I run the commands in the testscript.mos file from an OM Shell by changing to that directory, everything works fine. But If I run the batch file from the DOS prompt, I get the following error
Error processing file: testscript.mos
[C:/Users/blahblah/testscript.mos:2:1-2:1:writable] Error: Missing token: ASSIGN
# Error encountered! Exiting...
# Please check the error message and the flags.
Execution failed!
The HelloWorld.mo file comes with the standard installation and I haven't modified it
class HelloWorld
Real x(start = 1);
parameter Real a = 1;
equation
der(x) = - a * x;
end HelloWorld;
I am new to OpenModelica and searched online but couldn't find a solution. Any help is appreciated.
All the commands run in the OM Shell but not when invoked from the bat file.
The script code needs to be valid Modelica, so you need ; after each command.
loadModel(Modelica);
getErrorString();
loadFile("HelloWorld.mo");
simulate(HelloWorld);
getErrorString();

SCP command not working in karate project - it throws command error:cannot run program scp.exe: CreateProcess error=2 [duplicate]

I'm trying to execute bash script using karate. I'm able to execute the script from karate-config.js and also from .feature file. I'm also able to pass the arguments to the script.
The problem is, that if the script fails (exits with something else than 0) the test execution continues and finishes as succesfull.
I found out that when the script echo-es something then i can access it as a result of the script so I could possibly echo the exit value and do assertion on it (in some re-usable feature), but this seems like a workaround rather than a valid clean solution. Is there some clean way of accessing the exit code without echo-ing it? Am I missing on something?
script
#!/bin/bash
#possible solution
#echo 3
exit 3;
karate-config.js
var result = karate.exec('script.sh arg1')
feture file
def result = karate.exec('script.sh arg1')
Great timing. We very recently did some work for CLI testing which I am sure you can use effectively. Here is a thread on Twitter: https://twitter.com/maxandersen/status/1276431309276151814
And we have just released version 0.9.6.RC4 and new we have a new karate.fork() option that returns an instance of Command on which you can call exitCode
Here's an example:
* def proc = karate.fork('script.sh arg1')
* proc.waitSync()
* match proc.exitCode == 0
You can get more ideas here: https://github.com/intuit/karate/issues/1191#issuecomment-650087023
Note that the argument to karate.fork() can take multiple forms. If you are using karate.exec() (which will block until the process completes) the same arguments work.
string - full command line as seen above
string array - e.g. ['script.sh', 'arg1']
json where the keys can be
line - string (OR)
args - string array
env - optional environment properties (as JSON)
redirectErrorStream - boolean, true by default which means Sys.err appears in Sys.out
workingDir - working directory
useShell - default false, auto-prepend cmd /c or sh -c depending on OS
And since karate.fork() is async, you need to call waitSync() if needed as in the example above.
Do provide feedback and we can tweak further if needed.
EDIT: here's a very advanced example that shows how to listen to the process output / log, collect the log, and conditionally exit: fork-listener.feature
Another answer which can be a useful reference: Conditional match based on OS
And here's how to use cURL for advanced HTTP tests ! https://stackoverflow.com/a/73230200/143475
In case you need to do a lot of local file manipulation, you can use the karate.toJavaFile() utility so you can convert a relative path or a "prefixed" path to an absolute path.
* def file = karate.toJavaFile('classpath:some/file.txt')
* def path = file.getPath()

Execute external powershell script in jenkins pipleline

I was trying to execute powershell script in a remote machine via jenkins pipeline.my script location ex: $server_vm_name\d$\scripts\testscript.ps1 . and I need to pass parameter to the testscript.ps1 as parameter name -clientpath. for the client path u have used this external path ex:
\${client_vm_name}\d$\Client\Application . I have used the network paths for both script location and parameter
I have write the following code snippet to get the work done. but its not working. anyone have idea what i have done in wrong way?.
error msg :
*
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 885: unexpected char: '\' # line 885, column 49.
rshell script: "\\$db_vm_name\d$\script
stage('Installation') {
agent{
label PS_AGENT_LABEL
}
steps{
script{
powershell script: "\\$server_vm_name\d$\scripts\testscript.ps1 -clientPath \\${client_vm_name}\d$\Client\Application"
}
}
}

Running java utility via command in azure pipelines does not pass parameters

I have created an utility (java jar-file) to generate a file upon the building process. And placed it into /tools folder in my source code root folder.
In azure-pipelines I am using this approach to run this jar-file upon building the project (Xamarin project).
- task: CmdLine#2
inputs:
script: "java -jar $(build.SourcesDirectory)/tools/version2image.jar $(fullVersionText) $(androidVersionFileLocation)"
The task launched ok, but in the logs I get the following:
2020-08-17T14:48:45.4688112Z java -jar D:\a\1\s/tools/version2image.jar 1.0.0.123 'D:\a\1\s/XamarinProject/XamarinProject.Android/Resources/drawable/version.png'
2020-08-17T14:48:45.5231861Z ========================== Starting Command Output ===========================
2020-08-17T14:48:45.5731945Z ##[command]"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\d5706f7c-43cd-4d50-8f17-98a6ae0a3391.cmd""
2020-08-17T14:48:46.8000947Z Version 2 Image Utility. version 1.0
2020-08-17T14:48:46.8001744Z
2020-08-17T14:48:46.8002367Z
2020-08-17T14:48:46.8003059Z Usage:
2020-08-17T14:48:46.8004115Z
2020-08-17T14:48:46.8004468Z java -jar version2image.jar version_string output_file_location
2020-08-17T14:48:46.8004769Z
2020-08-17T14:48:46.8005096Z Example:
2020-08-17T14:48:46.8005464Z
2020-08-17T14:48:46.8005739Z java -jar version2image.jar 1.0.0.121 c:/myproject/images/version.png
2020-08-17T14:48:46.8005961Z
2020-08-17T14:48:46.8507661Z ##[section]Finishing: CmdLine
In other words Azure does not pass parameters properly to the jar file. My application thinks that it launched without parameters.
But when I run this utility locally on my computer it works ok.
If it helps, in the java utility I get command line parameters like that:
final int correctParamsCount = 2;
if (args.length != correctParamsCount) {
System.out.println("Usage:\n\njava -jar version2image.jar <version_string> <output_file_location>\n");
System.out.println("Example:\n\njava -jar version2image.jar 1.0.0.121 c:/myproject/images/version.png\n");
System.exit(0);
}
final String versionNumber = args[0];
final String outputFilePath = args[1];
How can I fix this issue?
To resolve this issue (thanks to #riQQ) I should update AndroidVersionFileLocation variable, and remove single quote symbols from the path.
And add double quotes to the utility command call:
It should look like this:
script: 'java -jar $(build.SourcesDirectory)/tools/version2image.jar "$(fullVersionText)" "$(androidVersionFileLocation)"'
instead of this:
script: "java -jar $(build.SourcesDirectory)/tools/version2image.jar $(fullVersionText) $(androidVersionFileLocation)"

Calling Jmeter Functions from BeanShell Assertion Script

I am trying to run the jmeter test-suites in eclipse.
In my test-suite I am using a BeanShellAssertion to count the number of rows in a csv file.
I have a custom jmeter function to do so.
The script of the BeanShellAssertion is :
String str = "${__CustomFunction("Path to the CSV file")}";
int i = Integer.parseInt(str);
if(i ==0)
{
Failure = true;
FailureMessage = "Failed!";
}
return i;
This test-suite works fine when I run it using the jmeter on my local machine.
Only when I try to run it with eclipse, (using the jmeter maven plugin) I see the following error:
jmeter.util.BeanShellInterpreter: Error invoking bsh method:
eval Sourced file: inline evaluation of: `` String str =
"${__CustomFunction("FilePath")}"; int i = Integ . . . '' : Typed
variable declaration : Method Invocation Integer.parseInt
I am wondering if there's some other way to invoke the jmeter functions when executing it using eclipse cause I am sure that the function is correct as I mentioned before that it works fine when the test suite is run using the jmeter on my local machine.
Any help would be appreciated.
Thanks.
Are you sure your custom function jar is visible for the Maven Plugin ?
As when you run it from JMeter, it works , I suppose you have a jar in lib/ext.
So you need to make this jar available to the jmeter maven plugin.