running java program from command line with classpath - java

Most of the time when i am learning something i am running java program from commandline.
now my problem is little complecated.
i am running Simple java program from command line. But i need CLASSPATH variable in environment variables. when i am adding CLASSPATH variable my program is giving me error.
The error is classNOtFoundException. Means when i am running program it will check .class file and it is going in my classpath variable and that is obvious it will not found and it will fire exception.
i am always working line this. this problem occurs after i have formatted my PC. but i am using same OS and same jdk as it was.
Any body have faced this type of issue?
it is not working with following Screen shot.
Working with following screen shot
Following is my desktop screen shot and all things are working with classpath and without classpath(Means i can run simple Program no matter if classpath is there or not.).

Try this,
1. Goto your JDK folder, and then go inside bin,
2. Copy the path
eg: C:\Program Files\Java\jdk1.7.0\bin
3. Paste this path in "PATH", NOT "CLASSPATH" .
4. Then i recommend giving the command "gpupdate /force" in cmd.
(I know it about updating policies, but that how it works for me....)
5. Then type the command "java -version" on cmd, if you get the jdk version in respose, you are good to go.

Try adding "." (current directory) to your system class path.
A better practice would be to create a batch file to set per-application environment variables and launch your program.
Please include the full text of the class not found exception and the system class path you have defined.

According to you your JDK is working properly (because u getting classNOtFoundException)
so defiantly the class which you want to run is not in class path
Lets try this..
java -cp /your/path ClassName
Other wise Lets take a look of this may its helps you.. PATH and CLASSPATH

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

Java on Win server, bat run packages

To begin with, I dont know much Java, but we have some old Java applications on my firm that collects information that runs every night, however recently it stopped working. There is a bat file that, from what I understand, runs all the required packages, however, when I run it, it returns "Could not find or load main class -insert whatever class I put second - "
Code from the .bat file:
java -cp com.spprod.mywysiwyg.NY.jar; mysql-connector-java-3.1.10-bin.jar; com.spprod.mywysiwyg.CalculateDailyStats;
The first one is the whole package, and the second one is the connector, they are both in the same folder, but in the cmd I get the error that it cant find mysql. If I switch order with 2nd and 3rd it says that I cant find "CalculateDailyStats" instead. Is there something wrong with this line of code?
Thanks in advance.
The -cp switch specifies the classpath in java (path where to look for the files). The jars after -cp are separated with semicolon.
Now, I suppose, that you are trying to run com.spprod.mywysiwyg.CalculateDailyStats class. So, maybe the last semicolons are confusing e.g. try the following:
java -cp com.spprod.mywysiwyg.NY.jar;mysql-connector-java-3.1.10-bin.jar com.spprod.mywysiwyg.CalculateDailyStats

Program compiling in Eclipse but not in Command Prompt

Basically, my program compiles and runs fine in Eclipse but doesn't compile in Command Prompt. It gives me Symbol Not Found with the pointer on the name of one of my classes. I've never had this happen before- the only difference between this program and the other ones I wrote is that I made a package in Eclipse for them. Any ideas?
I think you didn't set the path correctly.
How to set Path in Java
The path is required to be set for using tools such as javac, java etc.
If you are saving the java source file inside the jdk/bin directory,
path is not required to be set because all the tools will be available
in the current directory.
But If you are having your java file outside the jdk/bin folder, it is
necessary to set path of JDK.
If you did not do it already, it might help if you remove the package declaration in the java files. If your compiling class uses any other classes, they will not be compiled because of the package declaration. It happened with me once.

Starting with Java

So I am trying to start with Java (as in, trying to get the dang thing to accept code). I download all the needed things (the SDK) from Java and such, but when it gets to the point where I have to do "javac" in Command Prompt to compile the notepad file, I just get the message saying that there is no command called "javac".
Anybody wanna share some insight?
To set the environment variable PATH: http://www.java.com/en/download/help/path.xml.
Also I recommend using an IDE such as netbeans or eclipse. They make it much easier when starting off in java, plus when getting into advanced projects with many classes, they help greatly.
You need to set the path to your java compiler for it to be found when you use the command prompt. This page explains how.
I would suggest setting the following environment variable:,
JAVA_HOME to point to the root of your java installation e.g. C:\Program Files\Java
than append the following to your PATH environment variable:
;%JAVA_HOME%\bin
than you will be able to use java and javac from the command line.
Also see this article from Microsoft on setting environment variables if your not familiar with it.
You need to add the Java bin directory (where javac.exe is located, assuming you're on Windows), to your system PATH.
Right click on "My Computer", go to Environment Variables, and add the bin directory where Java is installed to your PATH variable.
You will need to have the java bin directory on your path. So, on windows, if installed at c:\java, and bin is c:\java\bin (normally you have version number, jre vs sdk, etc), you will need to add that to your PATH environment variable. set PATH=c:\java\bin;%PATH% -- you could do this in a setlocal/endlocal block or set it permanatly for your machine.
Also, the JRE may not have javac -- you may need to dowload the SDK.
Starting with java by typing 'javac' at the command line satisfy my mother's definition of the phrase 'starting with' ... as in,
"Don't start with me, buddy."
You are likely to end up with a punch in the nose.
For your own sanity, pick one of Eclipse, or NetBeans, or IntelliJ, or the other popular IDEs, and start from there.

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!!!

Categories