Open a file on Openshift using Java - java

I have a DIY cartridge. My project structure looks like
MY_PROJECT
-diy
-myProgram.jar
-resources
-file1, file2...
-.openshift
-action_hooks
-start
The myProgram.jar uses files from the folder 'resources'.
The code looks like
File imageFolder = new File("resources");
System.out.println("Image Folder read:"+imageFolder.canRead()); //canRead returns false
File[] listOfFiles = imageFolder.listFiles(); // here I get null
The program runs by action hook 'start':
nohup java -jar $OPENSHIFT_REPO_DIR/diy/*.jar --server.port=${OPENSHIFT_DIY_PORT} --server.address=${OPENSHIFT_DIY_IP} &
The problem is that I'm not able to work with files.
As described in code comments I get null on listFiles().
If I run the program on Openshift manually(ssh to server/$OPENSHIFT_REPO_DIR/diy/ and run java -jar ...) it works, but it doesn't work via action_hooks.
Thank you in advance!

I resolved the issue with Openshift env variable 'OPENSHIFT_REPO_DIR'.
Instead of using relative path
new File("resources");
I use absolute
String absolutePath = System.getenv("OPENSHIFT_REPO_DIR");
new File(absolutePath + "diy/resources")

Related

Java Spring Boot failing to find Python Script in resources folder

I am trying to call python Scripts(in resources) from my Java class.
This is my code spinnet
String res = "/Scripts/Brokers/__init__.py";
URL pathUrl = this.getClass().getResource(res);
String path = "";
if(pathUrl != null)
path = pathUrl.toString();
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3.6", path);
ProcessBuilder is giving error No such file or directory.
P.S.
value of path = "file:/home/path-to-project/project-name/out/production/resources/Scripts/Brokers/\__init__.py"
Also how to include python scripts in jar file to run the project on linux server.
I am stuck with this issue since last 3 days. Please suggest.
From java doc:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Starting a new process which uses the default working directory and
environment is easy:
Process p = new ProcessBuilder("myCommand", "myArg").start();
So basically, you going to fork a sub process that will
start a python interpreter
with the python script that you have provided as argument.
The path to your script should be a normal path that your OS can understand, therefore you should not give a URI? like path (protocol:address/path).
if (path.contains(":"))
path = (path.split(":"))[1];
Also the backslash \ before __init__.py looks suspicious.
You should be able to run ls /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/__init__.py and see the file, the same goes for ls /usr/bin/python3.6.

Running yarn job from java program using ProcessBuilder gives file does not exist error

I am trying to run a yarn job from a java wrapper program. The mapreduce jar takes two inputs:
A header file: I dont know the name of the file but the location and file extension and there's only one file at that location
A Input files directory
Apart from these I have an Output directory.
the processbuilder code looks like:
HEADER_PATH = INPUT_DIRECTORY+"/HEADER/*.tsv";
INPUT_FILES = INPUT_DIRECTORY+"/DATA/";
OUTPUT_DIRECTORY = OUTPUT_DIRECTORY+"/";
ProcessBuilder mapRProcessBuilder = new ProcessBuilder("yarn","jar",JAR_LOCATION,"-Dmapred.job.queue.name=name","-Dmapred.reduce.tasks=500",HEADER_PATH,INPUT_DIRECTORY,OUTPUT_DIRECTORY);
System.out.println(mapRProcessBuilder.command().toString());
Process mapRProcess = mapRProcessBuilder.start();
On run, I get the following error:
Exception in thread "main" java.io.FileNotFoundException: Requested
file /input/path/dir1/HEADER/*.tsv does not exist.
But when I run the same command as :
yarn jar jarfile.jar -Dmapred.job.queue.name=name -Dmapred.reduce.tasks=500 /input/path/dir1/HEADER/*.tsv /input/Dir /output/Dir/
It works all fine.
what can be the issue when running the command from java is causing this issue?
The * is being treated as part of the literal string in this case rather than a wildcard. Therefore globbing isn't expanding to your desired path name.
If there is only one file in the directory, why don't you find what its path is and pass that as the argument instead
eg.
File dir = new File(INPUT_DIRECTORY+"/HEADER);
if (dir.list().length > 0)
String HEADER_PATH = dir.list()[0].getAbsolutePath();

Giving command line arguments to java program over git-bash

I'm currently working on a program to zip and unzip files/directories and it behaves kind of weird when I call it over the git bash.
The program takes three arguments (zip/unzip, inputPath, outputPath).
For Example:
java -jar zip_unzip.jar --zip H:\\zip_test H:\\test5\zip_test_4.zip
Everything works fine when I call it over Eclipse or CMD. It creates the directory structure if it doesn't already exist and zips the input Folder into the newly created output folder. But when I call it over the git bash like in the example above it somehow "ignores" the backslash and instead of creating a folder called test5\, creates a zip-archive called test5zip_test_4.zip
Here's a snippet of the code that takes care of creating the directory structure, where zippedFolder is the outputPath-Parameter:
File directoryToZip = new File(inputFolder);
String targetZippedFolder = zippedFolder;
targetZippedFolder = targetZippedFolder.replace("\\", "/");
//create directory to store archive in, if it doesn't already exist
File destDir = new File(targetZippedFolder.substring(0, targetZippedFolder.lastIndexOf("/")));
if (!destDir.exists()) {
destDir.mkdirs();
}
In line 3 of the codesnippet I'm replacing every backslash with a forwardslash because I thought this would make it platform-independent.
Can someone explain the behavior of the git bash to me and maybe suggest a more platform-independent path handling method?

How can i get the correct path where it was double clicked or executed from in Java?

My java jar is installed in c:\program files<i do not know>\i do not know\here.jar. When the user execute my application.
I always get c:\uesrs\\here.jar and its failing.
String Windows7HomeEditionProgramFileDirectroyStruct = System.getProperty("user.dir");
How can i get the correct path where it was double clicked or executed from?
Details (tested both answer but none is saying where the jar is located):
Try 1)
new File(".").getAbsolutePath();
or
new File(".").getCanonicalPath();
C:\Users\sun>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1
\dist\JavaApplication1.jar"
C:\Users\sun\.
Try 2)
ClassLoader.getSystemClassLoader().getResource(".").getPath();
C:\Users\sun>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1
\dist\JavaApplication1.jar"
Exception in thread "main" java.lang.NullPointerException
at javaapplication1.JavaApplication1.main(JavaApplication1.java:18)
Follow up:
String test = JavaApplication1.class.getProtectionDomain().getCodeSource().getLocation().getPath();
C:\>java -jar "C:\Users\sun\Documents\NetBeansProjects\JavaApplication1\dist\Jav
aApplication1.jar"
/C:/Users/sun/Documents/NetBeansProjects/JavaApplication1/dist/JavaApplication1.
jar
You can get the current directory like that:
String path = new File(".").getAbsolutePath();
What you get using "user.dir" is the user directory.
Using user.dir will always give you the Users directory in Windows.
Try this:
ClassLoader.getSystemClassLoader().getResource(".").getPath();
The "user.dir" system property will return the working directory for the JAR file. The same is true for Binyamin's answer.
You are looking for the directory that contains the executing JAR file. A similar question was posed in this thread, the answer may help you.

Why can't I access a file within my jar unless I'm in the folder with the Jar when I run it?

I recently created an application and successfully jarred this to c:/my/folder/app.jar. It works like a charm in the following case [Startup #1]:
Open cmd
cd to c:/my/folder
java -jar app.jar
But when I do this, it doesn't work [Startup #2]:
Open cmd
cd to c:/my/
java -jar folder/app.jar
Because app.jar contains a .exe-file which I try to run in my application:
final Process p = Runtime.getRuntime().exec("rybka.exe");
It won't work in example 2 because it can't find the file rybka.exe.
Any suggestions?
Something like this is a better way forward. Copy the exe out of the jar to a temp location and run it from there. Your jar will then also be executable via webstart and so on:
InputStream src = MyClass.class.getResource("rybka.exe").openStream();
File exeTempFile = File.createTempFile("rybka", ".exe");
FileOutputStream out = new FileOutputStream(exeTempFile);
byte[] temp = new byte[32768];
int rc;
while((rc = src.read(temp)) > 0)
out.write(temp, 0, rc);
src.close();
out.close();
exeTempFile.deleteOnExit();
Runtime.getRuntime().exec(exeTempFile.toString());
If the jar will always be in that directory you can use a full path /my/folder/rybka.exe. If not, you can use getClass().getProtectionDomain().getCodeSource().getLocation() to find out the location of the jar and prepend that onto rybka.exe.
Try extracting the exe to
System.getProperty("java.io.tmpdir"));
then run it from this location too should work every time.
Paul

Categories

Resources