I'm not sure if it is best practice but I add MySQL-connector jar to the extensions directory of my Java install directory to I can easily connect to MySQL databases.
I also set environment variables to point to various directories so that I can develop on different machines and only define environment variables locally and code doesn't have to be modified for file paths.
In either case of the above I find that unless I reboot my computer java does not recogise either. What happens during a reboot to Java? Is some config file updates by a java process? Can you update this without having to reboot?
To test this I have created a new environment variable on both Mac (adding to .MacOS/environment.plist), Linux (Ubuntu 12.04) and windows 7 (via control panel). I then used System.getenv("TestVar"); which returns null. Running set from the command line shows it exists though. After a reboot System.getenv("TestVar"); returns the expected value.
Ultimately your goal is to include jar files in CLASSPATH . its up to you how include jars in classpath but this is not good practice to put jars inside extensions directory . While running your program modify CLASSPATH value .
java -cp jar1:jar2:jar3:dir1:. HelloWorld
java -classpathjar1:jar2:jar3:dir1:. HelloWorld
As far as setting environment variables goes the on Ubuntu a log out is required
https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting
Related
I have to set LD_LIBRARY_PATH such that one of the jar in my classpath uses that to execute some native java files.
I have tried setting up the environment file to my server while bootstrapping, but
System.getenv(LD_LIBRARY_PATH) is given as null.
Library path in use by Java is shown by System.getProperty("java.library.path"). Just print the value in your app when you run it.
java ... your.App
Note the value and edit LD_LIBRARY_PATH before running a second time:
export LD_LIBRARY_PATH=/blahblah
# or export LD_LIBRARY_PATH=/blahblah:$LD_LIBRARY_PATH
java ... your.App
You should see the changed "java.library.path" now prefixed with /blahblah:.
How do I setup https://jdk.java.net/10/ so Solr 7.5 can find it under the installation?
I am using linux Amazon Linux 2 AMI 2.0.20181008 x86_64 HVM gp2.
Questions
In what directory should I put the files. ex. /usr/local/java?
Where should I set JAVA_HOME so every user will get it also after a reboot.
Where should I set the path to java so every user will get it also after a reboot.
Any installation location should work as long as you set the correct JAVA_HOME path and add the binary path to your $PATH variable.
You can configure both globally by editing /etc/environment, which should take effect for all users after restarting your shell (you can shortcut that process by doing source /etc/environment when testing).
JAVA_HOME=/usr/local/java/jre_10....
There should already be a PATH entry there which you can modify to add the Java binary directory.
This can differ between different Linux distributions and shell versions. If your distribution (Amazon Linux used it earlier at least) doesn't support it, you can use /etc/profile.
Another option that many prefer is to add an executable shell script in /etc/profile.d/ that sets up the variables for you, as these scripts are invoked on each initialization of a shell for all users.
It could be basic java question. can we run java on my machine pointing to the other machine's JAVA Path?
Example: I have a test.class file in my machine (system-A). But my machine does not have java(no JDK or No JRE). Shall I run this test.class by using other system's java path?
Java does not need to be "installed" to function correctly. At least not the core Java, though some tools of Java may need extra setup.
If your machine can see a Java home directory on a network share, you can simply run java.exe directly from that share.
Example: If you have access to a network share like \\Server1\Software and it has a Java installed in directory Java\jdk1.8.0_65, then you can run your code like this:
\\Server1\Software\Java\jdk1.8.0_65\bin\java.exe -cp . test
This of course assumes that test.class exists in the current working directory, and that the class is in the unnamed package.
Using the above will work, even without setting up JAVA_HOME or CLASSPATH.
No, you must have access to a java executable to run java. The original post states that there is no java runtime environment on the computer.
If, however, you have access to java.exe then you can execute that java.exe
(I already examined the answers here and here, but that doesn't answers my question)
I copied Java's folders (JRE & JDK) to a local folder and I manage to run .jar application by this command:
SomeFolder\Java\jre6\bin\javaw.exe -jar SomeApp.jar
But how can I run .exe application (e.g. eclipse.exe) by a similar way in two cases:
The application required only JRE
The application required also JDK
p.s. unfortunately "portableapps.com" solution is not acceptable.
The problem with .exe files is that they typically search the Java runtime on their own and there is no generic way to tell all of them where it is located.
In case of eclipse you can though e.g. by editing the eclipse.ini file.
Add
-vm
/usr/lib/jvm/java-7-oracle/bin
to the beginning of the file (with your path) and it should now start using the runtime you have specified.
A generic solution would be to figure out how the exe file runs the java code and then run it manually via javaw.exe -jar - that might not be possible if the exe file has the java code included and extracts it on demand. There are on the other hand versions as plain jar (often the version for Linux) that don't need all the exe workarounds.
Long story short...
1) Copy an installed version of the JDK (from another computer) you want to run to the destination computer.
2) Create a batch file to create or modify your environment variables, such as CLASSPATH, JAVA_HOME, and PATH.
#rem CONTENT OF BATCH FILE
#echo off
#cls
SET JAVA_HOME= <-- your JDK location
SET CLASSPATH=.;%JAVA_HOME%
SET PATH=%PATH%;%JAVA_HOME%\bin
3) Run this batch file before you launch Eclipse, or any executable that depends on the Java location. (Don't close the Command window or these transient values will vanish.)
4) You can close the Command window when you no longer need it.
This has worked for me since most Java dependent programs look for the JAVA_HOME variable in your computer's environment the path to Java binaries.
Have fun!
I have a Java application hosted on a remote tomcat instance that executes a bat file which contains the following line
javac filename.java
I receive the following error :-
'javac' is not recognized as an
internal or external command, operable
program or batch file.
I have set the path in the environmental variables. I tried running the same bat file on the machine, it works(which means there is nothing wrong with the path). But running it through the application gives the error. What might be the problem?
Setting the correct path in your environment does not fix the path in the environment of the running tomcat process. If you changed the path in the system settings, a tomcat restart (or system reboot) probably solves your problem.
Once you add %JAVA_HOME%\bin to the PATH var on the remote machine (assuming you have %JAVA_HOME% set there), you will need to run up a new cmd window for the new PATH to be available.
You should set the classpath for the user who is executing tomcat, not only for your login user.
If you have the JRE installed, and not the JDK, you'd be able to run Tomcat but there wouldn't be a javac.exe. Go to JAVA_HOME/bin and see if there's a javac.exe. If not, you have the JRE installed, and you'll have to go get the JDK.
Just curious - why is your Tomcat app calling javac.exe? Are you creating classes on the fly? If yes, why would you not be generating byte code using ASM instead?