Set classpath permanently in Linux - java

I am using centos and I have 4 to 5 jar files which I want to add permanently in classpath, So that I can run my program from anywhere. I tried this-
export CLASSPATH="/path/to/file:/path/to/file2"
Above command worked perfectly but I think it was not adding files permanently. When I opened new tab and tried to compile my program then I was getting same error i.e jar files were missing.
Now, Please help me to add these jar files permanently to classpath.

You could add that export statement to the end of your bash init file ~/.bashrc, therefore it will in effect all the time.
By the way, a better setting of CLASSPATH would be
export CLASSPATH="/path/to/file":"/path/to/file2":"${CLASSPATH}"
this will also preserve the initial value of that environment variable.

Add your path to /etc/profile and reboot to take effect
export CLASSPATH="/path/to/file:/path/to/file2"

Related

Where should vecmath.jar go in MacOS(11.5.1) [duplicate]

This question already has answers here:
Import javax.vecmath
(6 answers)
Closed 1 year ago.
Preamble: So this all started with just trying to use javax.vecmath.Vector2d. I didn't have javax.vecmath so I spent a bit of time trying to get it, found that I needed to download Java3D.
After a long time of trying to download Java3D for Java (version 16.0.2), I eventually got it together with the vecmath.jar file landing in /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/lib/ext. This got rid of the error: package javax.vecmath does not exist error message.
Then, I got the message
<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.
.Error: Could not create the Java Virtual Machine.
this also wasn't letting me use any java commands in shell.
A bit of research and I concluded the solution to be moving (via Finder select and drag) j3dutils.jar, vecmath.jar, and j3dcore.jar over to lib and just deleting the lib/ext directory. I have gotten rid of the <JAVA_HOME>/lib/ext exists problem but back to error: package javax.vecmath does not exist.
I don't even know what to do know. I just want to use javax.vecmath. Am I going about this the totally wrong way? How can I get this to work?
Okay, I figured it out.
How to use javax.vecmath in Mac OS(11.5.1) with Java(16.0.2)
I am giving a description that sort of includes why I do things, skip to the TLDR at the bottom if you just want an answer.
Step 1: Download the latest version of Java3D
This contains vecmath, along with j3dcore and j3dutils. It will download a .zip file. Unzip the file and it will expand into a new directory with another .zip file inside, j3d-jre.zip. Unzip j3d-jre.zip and it will expand into a directory lib. Inside lib will be a subdirectory, ext, with three .jar files inside: j3dcore.jar, j3dutils.jar, and vecmath.jar. You can put these literally anywhere, just make sure you keep track of their location (I put them in ~/Library/Java/Extensions, this location is on the hard drive and will need an admin password to do anything–use
sudo unzip /path/to/j3d-jre.zip
if you are doing things in shell). You CAN put the ext directory in JAVA_HOME/lib/ but after Java 6, this will cause a problem.
Step 2: Change CLASSPATH
Java has no idea how to find vecmath.jar so you have to specify it.
Option 1: Specify CLASSPATH with every shell command
The simplest version is using
javac -cp ".:/path/to/vecmath.jar:" MyMainProgram.java
to compile and
java -cp ".:/path/to/vecmath.jar:" MyMainProgram
to run the program (you can also replace -cp with -classpath and it will do the same thing)
This option won't ever destroy your CLASSPATH but you also have to include the -cp command every time you compile and run a program that imports javax.vecmath.
Option 2: Specify CLASSPATH with every new terminal window
A little more lasting than -cp, you can define CLASSPATH such that any changes will only take place in that terminal window. Use this form:
export CLASSPATH=".:/path/to/vecmath.jar:"
Now when you call
javac MyMainProgram.java
java MyMainProgram
Java will see that CLASSPATH is .:/path/to/vecmath.jar and everything will compile and run without adding the -cp command.
The main downside of this option is that if you update CLASSPATH again, you have to remember to add the previous CLASSPATH (which you can see at any time with echo $CLASSPATH)
Option 3: Permanently add CLASSPATH to terminal
Enter the following into terminal:
open ~/.bash_profile
this will open a window that may or may not have code in it. Regardless of any pre-existing code, scroll to the bottom and add
export CLASSPATH=".:/path/to/vecmath.jar:"
This option holds the CLASSPATH in all terminal windows forever or until you change it (using any method above).
TLDR
Download Java3D for macOS
Unzip java3d-1_5_1-macosx.zip and open the directory it creates
Unzip j3d-jre.zip and open the new directory /lib/ and the subdirectory /lib/ext/
Move vecmath.jar, j3dcore.jar, and j3dmath.jar to ~/Library/Java/Extensions (this requires an admin password) or any other location
Run the following line in terminal:
open ~/.bash_profile
Go to the bottom and add the following:
export CLASSPATH="/path/to/vecmath.jar:$CLASSPATH"
import javax.vecmath.* to any .java program you want
The jar file can go where you want, moving it to your project's lib folder is good. The real issue is you need your classpath to point to it.
Here is a full explanation.
If you are running from the command line you don't need to set the classpath variable, you can provide it in the java command. It would be something like this:
java -cp lib/vecmath.jar Example
This assumes that the program you are working on has been compiled into a class file named Example.class. If you main method is in a package you will need to fully qualify the classname so it might look like:
java -cp lib/vecmath.jar com.demo.Example
You can list multiple jar files on the classpath, separated by a colon (:).
You can also ask for help in the command line by invoking:
java -h

how to compile JShop2 src

I'm working with JShop2 GitHub. I'm having trouble with the installation; it says:
After unzipping the JSHOP2 zipped file in any directory, set the
CLASSPATH environment variable to include (replacing JSHOP2_DIRECTORY
with the directory where JSHOP2 is unzipped):
in Windows: JSHOP2_DIRECTORY\bin\antlr.jar;JSHOP2_DIRECTORY\bin\JSHOP2.jar;
How can I set these environmental variables? I don't have bin folder as shown in the path --> JSHOP2_DIRECTORY\bin\
I just read that i need to compile src to get bin folder, but I don't know how to do that. Please help.
You can still add the environmental variables even if the bin folder doesn't exist yet. Just make sure you restart your command prompt after you added the new environmental variables, for them to be picked up.
As for building the source in Windows, looking at the readme, their make.bat seems to be missing. You can create an issue in github to notify them. Meanwhile, you can use something like MinGW to run make on Windows.
After installing MinGW, you may have to execute
copy c:\MinGW\bin\mingw32-make.exe c:\MinGW\bin\make.exe
if make.exe didn't get created during the installation. Then you should be able to build the project by executing make from your jshop2 root folder.

Why won't my batch files run since I changed a Windows "Environment Variable"?

I had a couple of batch files to help me with minor tasks (one killed a running Skype process and the other deleted a file before running an executable). Both were only one or two lines of simple code.
To run a program called Kernow I was told I needed to add a Windows environment variable called "Path" and set it to point to a Java folder (C:\Program Files (x86)\Java\jre7\bin) - the program wouldn't run until I had done this giving a Java error.
I already had an environment variable called path which pointed to a long file path so instead of adding a variable I simply updated that one.
Now neither of the batch files that were working fine before will work. Both will run with no errors but not produce results.
I have very little knowledge of how batch files work with Windows. Can I set another environment variable called Path that will enable me to run my batch files and run Kernow??
You probably messed up with adding the folder to your path.
Your path should look like this:
C:\Folder1;C:\Folder2
When you add Java, you need to append it and add a semicolon, like this:
C:\Folder1;C:\Folder2;C:\Program Files (x86)\Java\jre7\bin
You may have missed the semicolon or completely overwritten the path.
An easy command to do this, as mentioned by #unclemeat in the comments:
set path=%path%;C:\Program Files (x86)\Java\jre7\bin

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Adding to Tomcat Classpath With CATALINA_OPTS

I'm trying add a directory of jar files (or barring that, each jar file individually) to the classpath for a tomcat instance. The difficulty is that I can't actually modify the /conf/catalina.properties file for this particular problem. I can set the CATALINA_OPTS variable, ie:
export CATALINA_OPTS = "$CATALINA_OPTS
-classpath /path/to/lib/file.jar"
However, this does not seem to add the jar file to the classpath.
Is it even possible to modify the Tomcat classpath like this? I'm aware that the CLASSPATH variable isn't used at all.
You can place your jar files in
{tomcat.home}/lib
They will be loaded from there.
Take a look on script that runs your tomcat. Print classpath just before it runs java process. As far as I remember it is using variable CLASSPATH when composing the command line.
Tomcat has an extension folder, designed to contain these kinds of jars. If I recall correctly it is just a matter of copying your jars to this folder.

Categories