Running Java Jar via Jenkins - java

I have a Java jar file which takes two csv files as an input. I tried to run this jar via Linux command line and it works fine.
Here is what i tried on Linux command line:
java -jar /home/test/Download.jar
I am trying to do the same via Jenkins in Execute shell but I am getting error:
Couldn't load file: test1.csv
Couldn't load file: test2.csv
information possible empty
These are the csv files that Jar takes as input.
I have given chmod 777 permission to all files.

Probably that's because of the location of the test[12].csv files. Try printing out the getAbsolutePath() for those File entries that you want to load: I'm sure they will point to a non-existing location.
I suppose those files are "next to" your Download.jar file. Now when you're executing a Jenkins job, the actual working directory is the workspace of the job (check the console log of the job on the web interface for the details). Either copy the files there or use absolute references.

Related

try to run mapReduce jar file with Hadoop

I am trying to use java to write a MapReduce function to count the words in a csv file.
I am able to successfully build the project and I manage to create a jar file.
I'm using Hadoop for running everything and I successfully install it on my PC, manage to upload the csv file to hadoop server but when I try to run this command:
hadoop jar C:/MRProgramsDemo.jar PackageDemo.WordCount wordCountFile MRDir1
I keep getting the following error message:
try to run other programs I found online and other jar file but without succses. can anybody no what I do wrong?
Two problems.
First, you put the file at the root of HDFS, not your user folder, therefore, the parameter must be /wordCountFile
Second, it looks like you've made an executable JAR, so you can remove the class name from the parameters

Issue running commands stored in files from spring boot executable jar file

I have one spring boot application which exposes some rest services.It contains some sql files and some python files which are stored in files inside resources folder in folder structure resources/scripts/.
Some of them are sql whereas some of them are python scripts.
I am creating an executable jar files using spring boot application. So when I package the application using maven, all those files go inside jar files as expected.Now, In a function inside code , I have a command as follow
Process p = Runtime.getRuntime().exec(run generator/generate.py)
generate.py is basically a python file containing stuffs to do.When I run the application within intellij , it works fine . When I package it and try to execute the jar file , it is not able to find the file and complaining .
Error running generator/generate.py: Cannot run program "generator/generate.py" (in directory "file:/Users/bpokharel/code/oprime_fm_service/target/oprime-fmservice-V1.jar!/BOOT-INF/classes!/scripts"): error=2, No such file or directory
failed to parse or execute scripts/commands.txt:45 [run generator/generate.py]
My resource folder is as follow:-
resources/scripts/commands.txt
resources/scripts/generator/generate.py
commands.txt contains other commands. One of the command is generator/generate.py. So I am basically reading commands.txt and executing each scripts as defined in commands.txt
I am reading commands.txt using inputstream and then executing each commands.
I believe Runtime.getRunTime tries to execute the command in a directory.
, but for executable jar file I am not sure how I can supply folder name where to execute the command
Any help would be appreciated .
Here is somewhat related but I have to go one step further than this .
Classpath resource not found when running as jar

Batch file not executing the required commands in windows 8.1

I have created a batch file to set the path of a jar file whenever i need to use that jar file.But even after executing the file,the system is unable to recognize the jar file and when i compile my java program which uses that jar file,it gives compile time error(i.e the path is not set).
And when i simply use the classpath command in command prompt which i wrote inside the batch file,it works.
But i want to make a batch file so that whenever i need to set classpath,i can use that batch file.
Help will be appreciated.
Following is the batch file.
set classpath=jsoup.jar;.;%classpath%
Solution found by OP:
Thanks,I solved the problem.I just made a .cmd file containing the command.Now whenever i will need to set the path i will run that cmd file in command prompt.

2: No such directory error when running jar from bat file using scheduler

I am trying to schedule up java application using windows scheduler.
I have created bat file where is written: java -jar C:\....(full path)\myJar.jar
Java is using data folder, located right next to jar file, during execution. Everyone has access to the data folder so permission is not the issue as far as I understand.
The way I access to the folder in java is by setting path: "data\\test.csv"
Please note that bat file is located next to jar file and the data folder.
Interesting is that if I run the bat file manually then everything works fine, when I run it from scheduler, error occurs.
I have solved it by adding cd line to bat file. So before execution of jar, directory is changed.
Bat file would look like this:
cd C:(full path to directory)
java -jar myJar.jar
pause

java command line argument to make jar find files in two directories?

I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");

Categories