How do I set $PATH using PHP? - java

My site is being hosted on a shared server so I don't have su access. I needed to run a piece of code with java but it's not available on the server. So I got a self-extracting version of java and put it on the server in my home directory. Then I gave executable permissions to java and I try running the code. I have to use relative paths when running the file because of the restrictions of the server.
Trying to run the java file ../java/bin/java -jar 'javafile.jar' gives me the following:
error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory
I looked and libjli.so is located at ../java/lib/i386/jli/libjli.so. So I'm thinking that because I'm running java using a relative path it doesn't exactly know how to look for the other files. I'm hoping that if I can add absolute/path/to/java/bin to $PATH then this issue will be resolved.
So once I'm running my PHP, I can use dirname(__FILE__) to get the full path of my java bin directory. I've tried the following code:
exec('export PATH='.$bin_path.':$PATH', $output, $return);
print_r(array(getenv('PATH'), $output, $return));
Prints:
Array(
[0] => /usr/local/admin/bin:/usr/local/admin/bin/servers:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/bin:/usr/bin,
[1] => Array(),
[2] => 0
)
So nothing was added to $PATH, no output was given, and the command returned a successful exit value. Is it just the restriction of the server that is preventing me from getting this working?

Firstly, this is not going to work.
exec('export PATH='.$bin_path.':$PATH', $output, $return);
It will launch a child process with a shell, run the export command in the shell, and then the shell will exit. But the export command only changes $PATH for that shell.
I'm not sure, but I suspect that you need to use putenv.
I'm hoping that if I can add absolute/path/to/java/bin to $PATH then this issue will be resolved.
Well, it could only help if you used a simple command name for invoking the java command.
And it would be simpler to just run java using the full absolute pathname; e.g. "/absolute/path/to/java/bin/java"

Related

Code runs but doesn't open when converted to .jar file? [duplicate]

I want to execute my program without using an IDE.
I've created a jar file and an exectuable jar file. When
I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:
Error: Unable to access jarfile <path>
I use the command: java -jar Calculator.jar
How I created the jar:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
I had encountered this issue when I had run my Jar file as
java -jar TestJar
instead of
java -jar TestJar.jar
Missing the extension .jar also causes this issue.
Fixed
I just placed it in a different folder and it worked.
[Possibly Windows only]
Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:
java -jar myjar.jar
I was able to fix this by givng the full, quoted path to the jar:
java -jar "%~dp0\myjar.jar"
Credit goes to this answer for setting me on the right path....
I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.
I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:
java -jar C:\Path to File\myJar.jar
i had to write
java -jar "C:\Path to File\myJar.jar"
Just came across the same problem trying to make a bad USB...
I tried to run this command in admin cmd
java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
But got this error:
Error: unable to access jarfile c:\fw\ducky\duckencode.jar
Solution
1st step
Right click the jarfile in question. Click properties.
Click the unblock tab in bottom right corner.
The file was blocked, because it was downloaded and not created on my PC.
2nd step
In the cmd I changed the directory to where the jar file is located.
cd C:\fw\ducky\
Then I typed dir and saw the file was named duckencode.jar.jar
So in cmd I changed the original command to reference the file with .jar.jar
java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
That command executed without error messages and the inject.bin I was trying to create was now located in the directory.
Hope this helps.
None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.
Fixes:
Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.
The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.
Do the following steps:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
Then type java -jar filename.jar
This should work.
Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
java filename.java
If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.
to check this you can view extended attributes on the file
$ ls -l#
-rw-r--r--# 1 dave staff 17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms 619
com.apple.quarantine 68
You can then clear the attributes with
xattr -c file.jar
It can also happen if you don't properly supply your list of parameters. Here's what I was doing:
java -jar test#gmail.com testing_subject file.txt test_send_emails.jar
Instead of the correct version:
java -jar test_send_emails.jar test#gmail.com testing_subject file.txt
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
For me it happens if you use native Polish chars in foldername that is in the PATH.
So maybe using untypical chars was the reason of the problem.
sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work
My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.
Disregarding relative file paths fixed it right up.
In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved
It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ".
For example:
java -jar c:\my folder\x.jar -->
java -jar "c:\my folder\x.jar"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.
Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.
"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar
If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).
A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?
For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.
So i used:
%Java8_64%\bin\java -jar myjar.jar
Instead of java 7 version:
java -jar myjar.jar
I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.
Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.
this is because you are looking for the file in the wrong path
1. look for the path of the folder where you placed the file
2. change the directory cd in cmd use the right path
I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!
Maybe you have specified the wrong version of your jar.
I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
Rename the jar file and try
Explanation :
yes, I know there are many answers still I want to add one point here which I faced.
I built the jar and I moved it into the server where I deploy (This is the normal process)
here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue.
maybe at the time of overriding there can be a permission copy issue.
Hope this will help someone.
Have you tried to run it under administrator privoleges?
meaning, running the command in "Run As" and then select administrator with proper admin credentials
worked for me
I was trying this:
After giving the file read, write, execute priviledges:
chmod 777 java-repl.jar
alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"
Unable to access bla bla..., this was on Mac OS though
So I tried this:
alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"
This did not work "Unable to access jarfile"
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"
This does work
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"
The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.

Error: could not find libjava.so while checking for jps

I am using centOS 6.10
ls /usr/lib/jvm
O/P:
java-1.6.0-openjdk-1.6.0.41.X86_64
java-1.7.0-openjdk-1.7.0.181.X86_64
java-1.7.0-openjdk-1.7.0.261.X86_64
java -version
O/P:
java version "1.7.0_181"
while checking for jps I am getting like this,
jps
O/P:
Error: could not find libjava.so
Error: could not find Java SE Runtime Environment.
My bashrc file be like,
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261.X86_64/
export HADOOP_INSTALL=/usr/local/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin
I dont know why this error is popping. I am getting frustrated because of this I am searching the solution for more than 3 days. Any help would be much appreciated.
Thanks in advance!!
I found java-related processes are sometimes quirky when it comes to libraries.
First identify the path for libjava.so and confirm the lib and executable are the same, one of 32- or 64-bit:
file /path/to/libjava.so /other/path/to/jps
Next, for any process like jps as an example, run this:
ldd /some/path/jps
The runtime link-editor should list an abs path for each lib referenced by the executable, or an error if not found. When there is an error, the lib is missing or exists in a directory that's not within the link-editor's search path. For normal processes, setting LD_LIBRARY_PATH usually works, but java stuff is too often quirky. Try experimenting with cmd-lines or a script, eg:
#!/bin/bash
LD_LIBRARY_PATH=/usr/local/lib64 /full/path/to/jps $*
(replace /usr/local/lib64 with the leading path to libjava.so).
Note that an independent "export KEY=val" is not required, and would add info into the environ and gets inherited by any process that follows; as shown, the shell sets KEY=val only for the cmd-line.
Some java-related quirks are processes that either clear the environ or reset stuff like LD_LIBRARY_PATH within their own child process(es), or call execve() with a NULL envp, and the child then fails as you describe. In that case you may have to resort to moving libs to a specific dir, or modify a java-related config file which lists lib dirs.
Sometimes quick answers can be found with strace, made easier when limiting the output, eg:
strace -f -e execve,open jps

How to find file path to a bash file when the folder is renamed or moved to a new drive?

I've made an executable jar file for a terminal game that can be opened by typing java -jar name.jar in the Terminal.
Then I made a .sh file inside the same folder as the jar file to open it by double-clicking the .sh. I asked how to do this here, where people told me to use the following code in the .sh.
#! /bin/bash
DIR=$(dirname "$0")
java -jar "$DIR/game.jar"
This worked for a while, but when I renamed the folder, I realised if I move the folder to a pen drive the whole thing stops working and I get this in the Terminal.
Error: Unable to access jarfile /Volumes/Hard
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So how to find the file path to the folder the .sh and the jar are in, regardless of where it is, what its name is and what drive it is on?
Also, I'm using MacOS Mojave 10.14.4 if that's of any importance.
The error looks like the path does contain spaces, like probably /Volumes/Hard Drive/Users/something. The solution is to quote the command substitution.
Tangentially, don't use upper case for your private variable names.
But of course, the variable isn't really necessary here, either.
#!/bin/sh
java -jar "$(dirname "$0")/game.jar"
Nothing in this script uses Bash syntax, so it's more portable (as well as often slightly faster) to use sh in the shebang. Perhaps see also Difference between sh and bash
You can store the full path of the working directory using the environement variable $PWD, like in this example (done in 5min, it is just to show you how it is works) :
#!/bin/bash
DIR=$PWD
gamePath='java -jar '$DIR'/game.jar'
echo $gamePath
Wherever I will execute this script, it will shows up the working directory even if I change the name of the parent. Let me show you :
You can see that $PWD environnment variable works great.
Now, I will change the directory name from TestFolder to TestFolderRenamed and execute the script again :
So, in your case, change your code as following :
#! /bin/bash
DIR=$PWD
java -jar "$DIR/game.jar"
It should works.

Trying to use Apache Daemon jsvc

I am trying to run a Java program by using jsvc.
I have installed it by
sudo apt-get install jsvc.
To find out a solution, I tried to read the Apache documentation about it (at https://commons.apache.org/proper/commons-daemon/jsvc.html). But this command:
./jsvc -cp commons-daemon.jar:my.jar MyClass
and this other:
./jsvc -cp my.jar MyClass
did not work (of course, I replace the terms by the name of my class etc.).
It gives me the error:
bash: ./jsvc: no such file or directory of this type
So I use jsvc without "./". And I saw here: How to start tomcat with jsvc? that I should use /usr/bin/jsvc
But an other problem is when I use
/usr/bin/jsvc -cp path/to/my/.jar path/to/my/class
nothing happens.
I try the link: How to convert a java program to daemon with jsvc?. But there is something I don’t understand: for the "CLASS =", have I to put a .Main file ? And do I have to put the extension name of the file (for the class and the .jar) ?
I decided to put the .java file which contains my main class (once I putted the .jar, then I didn’t). Then I copied the code, and when I write "esac" and pressed the enter key in the Ubuntu console, the console closed up, and then…nothing.
Has someone already encountered this ?
Are you sure your java installation is in /usr/java?
Beside this, in the second command there's the directory missing. You should do something like that:
export JAVA_HOME=path/to/java/home
./configure
If you don't know where your java installation is located, try this if you are on a mac/*nix, or this if you have windows.

Mac User - How do I set CLASSPATHS in Mac (I'm working on a Lucene Demo)

I'm trying to get my Apache Lucene demo to work and I'm down to setting the classpath in this tutorial http://lucene.apache.org/java/2_3_2/demo.html
I've hunted the web and these wer the 2 solutions I found to set CLASSPATH:
CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
and
setenv CLASSPATH ${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
The second one brings up a error
-bash: setenv: command not found
The first one seemed to accept ok but wen i tried the next step in the tutorial i got an error. The next step was to run the following:
Phil-hunters-MacBook:webapps philhunter$ java org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src
which gave me the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles
This leads me to believe my CLASSPATHS didnt set correctly. Would I be right in assuming this? I have tried other tutorials and demos and see to get this same error quite a bit. Im new to Lucene and relatively new to mac and Unix shell scripting. Anyone know if I am setting the CLASSPATH correctly and if thats the cause of the errors?
in the terminal type
$ vim ~/.bash_profile
edit the file and add one line:
export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.2/lucene-core-3.6.2.jar:/usr/local/lucene-3.6.2/contrib/demo/lucene-demo-3.6.2.jar;
make sure to change the path of yours.
In your way you lose to add lucene-demo-3.0.3.jar in your classpath.
When you set an environment variable like CLASSPATH then by default it only applies to the current process (i.e. the shell process itself) - it isn't available to the java process you launch in the next line. In order to make it available to other processes you need to "export" the variable. In this case you can use something like:
export CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar
This basically says "set the CLASSPATH variable to its current value plus the location of the lucene jar, and make the new variable available to any processes launched from this shell".
However, with java the usual way of setting the classpath is to do it as part of the java command itself, using the -classpath or -cp options. In your case it would look something like:
Phil-hunters-MacBook:webapps philhunter$ java -cp /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src
As an aside, the error you see when using the setenv line is because setenv is the command used in the C shell to set environment variables, but the default Mac shell (and the shell you're using) is bash which doesn't recognise setenv and lets you know it doesn't recognise it with the error message: -bash: setenv: command not found.
i create a .bash_profile file in my home directory and do things like
export GRAILS_HOME=/usr/share/grails
...
export PATH=${GRAILS_HOME}/bin:${GROOVY_HOME}/bin:/usr/local/mysql-5.1.45-osx10.6-x86_64/bin:${PATH}
you can work of that to set the classpath -- these examples show how to declare an environment variable and how to use the variable in other variables.

Categories

Resources