Run Java Jar within Python Google Cloud Function - java

I'm trying to port over some old Ruby code I used to run on Heroku to a Python-based Google Cloud Function.
This code runs Apple's Reporter tool which is "a command-line tool that you can use to download your Sales and Trends reports and Payments and Financial Reports". Docs can be found here.
The Ruby code worked well for years until yesterday, running on Heroku with a Ruby + Java build pack. A small snippet of this, where options are args received :
options = [
vendor_id,
file_type,
sub_file_type,
'Daily',
trimmed_date,
version
]
Dir.chdir("#{Rails.root}/tmp/") do
stdout, stderr, status = Open3.capture3("java -jar #{Rails.root}/public/jars/Reporter.jar p=Reporter.properties m=Robot.XML Sales.getReport #{options.join(', ')}")
return {:status => status, :error => stderr.to_s, :stdout => stdout.to_s }
end
The error I'm seeing on Heroku after no code or stack updates is Network is available but cannot connect to application. Check your proxy and firewall settings and try again.
Most of our other similar processes have been moved to Google Cloud Functions, so after getting nowhere with the above error I thought I'd move this also.
So a similar snippet this time in Python:
from subprocess import Popen, PIPE
def execute_reporter_jar(vendor_id, trimmed_date, file_type, api_version):
process = Popen(["java -jar Reporter.jar p=Reporter.properties Sales.getReportVersion Sales, Detailed"], stdin=PIPE, stdout=PIPE, shell=True)
out, err = process.communicate()
print("returncode = %s", process.returncode)
print("stdout = %s", out)
print("stderr = %s", err)
This works well locally, but when I deploy to Gooogle Cloud it seemingly runs successfully in a few ms, however, nothing actually happens and when I dig deeper it seems the subprocess is returning a 127 - command not found error. So it seems the cloud function can't access Java.
After a good 24hrs, I've hit a wall with this. Can anyone help? I have zero Java knowledge and I know cloud functions have a Java runtime, but I would prefer to stick with Python.
The ultimate aim is for Apple's reporter to run and save the requested file to Google Cloud Storage.
Thanks in advance!

The execution environment for Cloud Function's with Python runtime (both 3.7 and 3.8) is currently based in Ubuntu 18.04 (check the information in this link).
The runtime only includes the following system packages and running subprocess is usually not a recommended idea as the system packages included are limited.
If it's paramount for you to stick with Python you could try to deploy your function using the BuildPack CLI and extending the builder image to install Java on the Python runtime or if your application can be dockerized consider building an image yourself with Java included and deploying your application in Cloud Run.

Related

Opening app on macOS 11 Big Sur from JavaFX application randomly fails with kLSNoExecutableErr

We have a desktop JavaFX application (well, TornadoFX) that downloads an archive, extracts another app from it and launches this app with macOS open command.
Simplified kotlin code looks like:
ProcessBuilder(listOf("open", "/path/to/app.app", "arg")).start()
This has worked for years on older versions of macOS (10.15 and earlier) but now with macOS 11 Big Sur launching the app sometimes succeeds and sometimes fails.
In the mac Console.app following error can be seen:
OSStatus _LSCopyApplicationNodeFromOpenState(LSOpenState *): Returning kLSNoExecutableErr because node is a directory but we failed to register with error -10814
We extended the logic to check if all the files are really there before launching the app, and the files existed.
There is an assumption that maybe Launch Services database is not updated fast enough.
Following ways of trying to log what might be happening, didn't reveal any errors:
lsappinfo listen +all forever
log stream --debug --predicate 'subsystem == "com.apple.coreservices.launchservices"'
Does anybody have a clue if there is a way to avoid this behavior and to be always able to launch the app?
After a lot of research and debugging, what seem to have worked for us, was to force Launch Services to register the app in its database by executing command like:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /path/to/app.app
and afterwards we could launch the app.
The idea for such solution was found in this answer.

Appium IOS tests running when server started manually but not when server is started via AppiumDriverLocalService

Attempting to run Appium automation scripts on iOS (simulator) on a Mac Mini (M1 chip, if that's relevant). When I run the tests, they work just fine when the Appium server is started manually (typing "appium" into the terminal and starting it that way). However, when I attempt to start the appium server programmatically, the application under test fails to launch, with the following error:
2021-02-17 03:41:27:256 [W3C] WebDriverAgentRunner-Runner.app (19077) encountered an error (Failed to load the test bundle. If you believe this error represents a bug, please attach the result bundle at /Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Logs/Test/Test-WebDriverAgentRunner-2021.02.16_22-41-22--0500.xcresult. (Underlying Error: **The bundle “WebDriverAgentRunner” couldn’t be loaded because it doesn’t contain a version for the current architecture. The bundle doesn’t contain a version for the current architecture. Try installing a universal version of the bundle.** dlopen_preflight(/Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/WebDriverAgentRunner): no suitable image found. Did find:
2021-02-17 03:41:27:256 [W3C] /Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/WebDriverAgentRunner: mach-o, but wrong architecture))
Here's the full appium log: https://gist.githubusercontent.com/fida10/44344b223874310cf296d38a95d4268f/raw/316855b619129680eeaa6519a446a436d0699cd6/failedLog.txt
I originally thought that this was an issue with xcode or WDA, but if that were the case, the tests would fail no matter how Appium was started, and as mentioned previously, the tests pass perfectly fine when Appium is started manually (via terminal), so it might be an issue with the PATH or environment variables upon execution from Java, not sure though.
Here is the code I am using to start the server programmatically:
HashMap<String, String> environment = new HashMap();
environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
AppiumDriverLocalService server = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.withEnvironment(environment)
.usingDriverExecutable(new File("//opt/homebrew/Cellar/node/15.8.0/bin/node"))
.withAppiumJS(new File("//Users/sagolGoru20/.npm-packages/lib/node_modules/appium/build/lib/main.js"))
.usingAnyFreePort()
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File("//Users/sagolGoru20/Programming/JavaProjects/MavenProjects/MobileAutomationProjects/firstAppiumProject/appiumLog.txt"))
);
server.start();
As the appium log shows, the server seems to be started fine but then throws the above error, so I think the issue may be with how I'm building the AppiumDriverLocalService object.
I followed this tutorial in building AppiumDriverLocalService: https://appiumpro.com/editions/71-starting-an-appium-server-programmatically-using-appiumservicebuilder
Here is the project code. It's a simple project, I'm just clicking on an "Allow" button (line 26): https://gist.github.com/fida10/bec187a516fc32f907f97725263a7206
When I comment out the AppiumDriverLocalService server object (lines 46 to 58) and instead launch by uncommenting line 60, the test runs properly.
Any help would be greatly appreciated.
Was able to solve it by setting all options under "Build Active Architecture only" to "no", in XCode build settings. Details: https://github.com/appium/java-client/issues/1444#issuecomment-781078298
Hopefully this helps someone trying to run XCUITests on Mac devices with the new M1 chip.

service fabric java application deployment failed in local cluster

I am new to service fabric and trying to deploy java application to local service fabric cluster with 5 nodes. i am using Ubuntu VM and following below steps to build and deploy it in asf cluster. while deploying i am getting below error. i tried to deploy in asf remote cluster also and got the same issue. Can you please help me on this.
Link :Jav Application deploymen to ASF cluster
Error code:
Just tried this out and it worked for me so just going to ask some questions to make sure we didn't miss anything from the documents.
Under DhrumilSpringServiceFabric->DhrumilSpringGettingStartedPkg -> code, do you have two files?
gs-spring-boot-0.1.0.jar
entryPoint.sh
The entryPoint.sh file should have the following contents:
#!/bin/bash
BASEDIR=$(dirname $0)
cd $BASEDIR
java -jar gs-spring-boot-0.1.0.jar
Additionally, in the ServiceManifest.xml (located in DhrumilSpringServiceFabric->DhrumilSpringGettingStartedPkg), there should be the following snippet:
<CodePackage Name="code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>entryPoint.sh</Program>
<Arguments></Arguments>
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
The Program property value "entryPoint.sh" has to be identical including casing with what's in your "code" folder.
If the above all check out, then please respond and happy to dive deeper into this.
#Dhrumil Shah, I replicated the steps provided in the document and was able to achieve the desired results successfully.
Can you let me know if your java application is working fine without using service fabric and if you are using cli for your deployment?
Also, please check if java is installed properly on your VM. Check the below link for more information:
Java Webapp Deployment in Azure Service fabric explorer
I found the issue after spending some time in ASF logger. The issue was my YO generator was not working properly. i mean Yo json file was correpted. i run yo doctor and corrected it. its work

heroku nodejs app - events.js:167 Error Unhandled 'error' event : spawn java ENOENT

I'm having a node-js app on Heroku using the pdfMerge.js library.
following the documentation I'm using the stream event mechanism as a callback to identify the end of the process
then an exception is thrown :
events.js:167 Error: spawn java ENOENT.
I'm almost sure it's happening because I'm messing required java installation as described here:
pdfmerger combines multiple PDF-files into a single PDF-file. It is a node module that utilizes the Apache PDFBox Library, which the required functionality are distributed along with this module. The only requirement for this module to run, is having Java 6 or higher in the path.
I'm Not familiar enough with Heroku installation/configuration process in order to make it work.
thanks in advance
You can add Java to your app by adding the heroku/jvm buildpack like this:
$ heroku buildpacks:add -i 1 heroku/jvm
Then redeploy with git commit --allow-empty and git push heroku master.

Working with Php-Java Bridge

I am having trouble setting up the Php-Java Bridge setup properly.
I will explain what I have done.
My site is in pure php
For our payment transaction process we need to set up a php-java bridge
I followed this link to setup the bridge PHP-JAVA BRIDGE INSTALATION.
Here I learned that I need to have a private jvm to install the bridge.
So 1st i installed apache-tomcat-6.0.14 in Private JVM using my c-panel. After instalation it asked me to Map a domain to private JVM. So I mapped my domain example.com (which is the only option available) to it.
Then it asked to enable a traffic redirection from Apache web server to my Java application server (there was a check box and i clicked it)
Finally it asked me to deploy the WAR File (JavaBridge.WAR was my file) and everthing seems fine
Now when i go to http://example.com/JavaBridge/ I could see the javabridge examples and it works fine.
SO FAR SO GOOD
Now my problem starts here when I try to access a java class file from php. A sample test.php is what I create and put the following code into it.
<?php
require_once("http://example.com:portnumber/JavaBridge/java/Java.inc");
$System = java("java.lang.System");
echo $System->getProperties(); //This Part echo's correctly and shows the data so it means i can access Java.inc Correctly
$path_e24class = getcwd(). '/e24PaymentPipe.class'; //This part fails both test.php and java class file e24PaymentPipe.class are in the same directory in publich_html folder
java_require($path_e24class);
$pipe = new Java("e24PaymentPipe");
$pipe->setAction("1");
?>
My site contents reside in the public_html folder and the WAR file are deployed in private jvm.
These are the error message am getting.
1) Warning: java_require() not supported anymore. Please use tomcat or jee hot deployment instead
Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new e24PaymentPipe. Cause: java.lang.ClassNotFoundException: e24PaymentPipe VM: 1.6.0_22#http://java.sun.com/" at: #-10
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358) #-9
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204) #-8
java.lang.Class.forName0(Native Method) #-7
java.lang.Class.forName(Class.java:247) #-6
php.java.bridge.Util.classForName(Util.java:1518) #-5
php.java.bridge.JavaBridge.CreateObject(JavaBridge.java:445) #-4
php.java.bridge.Request.handleRequest(Request.java:458) #-3
php.java.bridge.Request.handleRequests(Request.java:500) #-2
php.java.bridge.http.ContextRunner.run(ContextRunner.java:145) #-1
php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:60) #0
http://example.com:portnumber/JavaBridge/java/Java.inc(232): java_ThrowExceptionProxyFactory->getProxy(3, 'java.util.Prope...', 'T', false) #1
Finally I don't know much about the java. So am stuck here not knowing what to do.
Here is a great step by step tutorial you can follow, which shows everything required! It is a little old (2007) but helped me a while ago.
There is also another option. You can install Apache Tomcat and deploy your war there. You can have even multiple tomcat instances simultaneously with your httpd running at the same time on the same machine, as long as you respect the port settings. You can even front them with Apache httpd.
you can try this:
package your code to jar, and copy it to java.ext.dirs which you can found in JavaBridge.log
copy the related class libraries to java.ext.dirs
restart the service of JavaBridge
good luck!
<?php require_once("JavaBridge/java/Java.inc");
try {
$hd = new java("hdfs.HDFS");
$hd->get("hdfs://master:9000/user/hadoop/test-in/logo_cn.png", "/home/hadoop/1.png");
} catch (JavaException $ex) { echo "An exception occured: "; echo $ex; echo "<br>\n";}
?>
You can use this php implementation on github that works with php 5.3.
See credits on the git readme for more information.
You can try this; put the JavaBridge.jar in tomcat's lib folder e.g. apache-tomcat-7.0.12/lib.
Restart tomcat server and then,
$pipe = new java("com.aciworldwide.commerce.gateway.plugins.e24PaymentPipe");
$pipe->setAction("1");
This way I created the php version of the object.
Why don't you put the e24PaymentPipe class in your Java application's classpath and skip the two lines below:
// $path_e24class = getcwd(). '/e24PaymentPipe.class';
// java_require($path_e24class);
$pipe = new java("fully.qualified.classpath.e24PaymentPipe");
You are mixing PHP side and Java side operations. in theory the java_require (which is deprecated) was designed to work on the Java side. You are specifying a PHP side path.
You can save yourself a lot of grief by using a pure PHP implementation of the e24PaymentPipe library.
Disclaimer
The link is to my github repo of the library, but I did not write it. See the readme in for original credits.

Categories