I have created some 4 to 5 Java files. They work well when I run using Netbeans or Eclipse, but when I try to run using command prompt I get some errors.
I have put all my files in jdk/bin folder.
I am using MySQL connector jar file - I have put that file in jdk/bin folder
I have set my class path to jdk/bin folder
But I get errors like:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Dbconnection.Dbconnect(Dbconnection.java:29)
at fileoperation.parsefiles(fileoperation.java:63)
at fileoperation.main(fileoperation.java:127)
Will anybody tell - where is the problem?
Here is what you have to do.
1.It is not a good idea to place source files in the jdk bin directory.so undo that.
2.create a folder say "apps" in your c:(hard disk) if you are using Windows.Exactly this way. C:\apps
3.Save all your source files in that apps folder.
4.Go to your environment variable and create a classpath variable under "user variables for ..."
Click on the new button, under variable name and type "classpath" (without the quotes)
under "variable value",supply the mysql connector/j driver exactly like this with the jar file at the end. C:\mysql-connector-java-x.x.xx\mysql-connector-java-x.x.xx-bin.jar(x marks the version number)
5.click ok.
6.Set the path for your jdk so that you don't keep setting it over and over each time you want to run your program.
C:\program files\java\jdk1.x.x\bin copy this address path from your windows explorer's address bar and put a semicolon (;) before pasting it in the path. You have to do this in your path system variables else,you will end doing this each tme you want to execute some codes.
cd\
cd apps (enter)
set classpath=(enter)
set path=c:\program files\java\jdk1.x.x\bin(enter)
javac ClassName.java(enter)
java ClassName(enter)
(Am so sure you don't want that all the time )
Now to compile,open your command prompt and change directory to your apps directory and type javac to test if your configs did work.You will get a list of the javac options.If you did then you are on the right track again.
open your jdbc code make sure these are rightly coded.
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:portnumber/test","username","password");
compile and run your code again.
Let me know if this helped you
First of all try running java and javac from command propmt, if they run properly its fine.
than Try this:
javac -cp /path to/your/mysql connector jar file/ MyProgram.java
java -cp .;/path to/your/mysql connector jar file/ MyProgram
cp stands for classpath
You can also add multiple jar files after -cp.
if they don't run i mean java or javac not found error comes up than add java.exe and javac.exe to your windows environment variable
Add C:\jdk1.6.0_23\bin which contains java.exe and javac.exe to your environment variable.
You can't set the directory in the classpath, you either need to specify "*.jar" under the directory, or specify each jar by name.
Related
I'm getting the following error when I try to run the 'jar' command in the command line on windows :
'jar' is not recognized as an internal or external command
The general solution seems to be that I need to add the jdk bin folder to the PATH in environment variables. I have already done this, I added the following to the path variable :
...; C:\Program Files\Java\jdk1.8.0_40\bin\;
Though I'm not sure if having the jdk reside in 'program files' instead of 'program files x86' affects this. I'm on 64 bit windows with 64 bit java.
Thanks
The path should only contain directories where the OS will look for executables. Strip the trailing "\jar.exe" to set your path as:
(old path variables here);C:\Program Files (x86)\Java\jdk1.7\bin
Thanks To : #stevevls
Try using this command:
java -version
If it doesn't work that means you failed to properly add path to jdk.
If it does work, try using:
java -jar
The jar command in command line is used in order to create a JAR file. For example:
jar cf jar-file input-file(s)
See more at: Oracle docs
If you want to run the existed JAR file you should use the java -jar command mentioned by #Aleksandr.
But in your case it looks like you don't have an access to the added directory in your path with JKD binaries so at the beginning try to execute mentioned java --version to check whether you have java in you classpath at all.
Please remember that on Windows you have to restart the console when you change the windows default path. You do not see the changes on the current console.
The x64 version of Java is installed on Program Files. The x86 version is installed on Program Files x86 by default.
Five steps to solve this problem:
check whether in your bin folder jar.exe is availbale or not
check for your environmental setting
download jar file from here http://originaldll.com/file/jar.exe/29248.html and keep in bin folder
now check for jar it will 100% work
keep any jar in your desktop and go to desktop path on cmd prompt and run the command jar xf filename.jar
Now see if it is working
I am facing a very strange type of java classpath problem. I have added path d:\javaext in the java classpath environment variable and when enter on the command prompt echo %classpath% , it shows the said path in the printed list but even then, when I try to compile a java file in which I am importing the package by adding import org.apache.commons.lang.*;, it some how says that the said package doesn't exist. The relevant jar file physically exists on that path also. Any clue that what am I missing, although it's a very basic stuff. I have also tried compiling it with setting the classpath on runtime by javac -cp or javac -classpath switch but even then no success.
I am trying to compile the code with java 1.8 on windows 8.1
Can Anybody tell that what this alert means in the property dialog box of the jar file?
Your .Jar file might be corrupt.
Else on Windows try to set path through Environment Variable.
And on Linux , export the path for temporary usage or set in bashrc or bash profile.....
This could be caused by differences between version of a certain "jar", some third-party libraries does not support backward compatibility. So, try browsing through the "jar" to find the package-class combination that you are using.
Hope this helps,
Brian
Is it safe to assume that the are no spaces inside the path, e.g. "C:\Program Files"? If there are spaces, try surrounding the classpath variable with quotes.
I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.
I've been trying to execute the commands to run the RMI Hello world example but I Failed!
My execution steps are taken from here: http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html
The commands are:
javac: works fine and I get the class files
rmiregistry &: I get something like [1] 17122
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ example.hello.Server:
Gives me an error message
Exception in thread "main" java.lang.NoClassDefFoundError:
example/hello/Server Caused by: java.lang.ClassNotFoundException:
example.hello.Server at
java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I did solve this problem by running each command on a new terminal window.
First I started with javac for the .java files, then run the command rmiregistry &.
Second I opened a new terminal window and run the command java for the Server class.
Third I opened a new terminal window and run the command java for the Client class.
and it works with me!
On the Mac, the Users directory in the root directory is written with a capital letter.
You wrote:
file:/users/ha/RMI/
file:/Users/ha/RMI/ would be correct. This applies only to file systems that have been setup explicitly to setup to ignore case (Case-insensitive). At least that is what the comments below are saying. I cannot verify this myself.
A classpath is also needed under many circumstances. It depends on your dependencies. But if you needed the classpath in windows, you will need a similar one on Mac.
Depending on where you .class files are, I would add the classpath to your invocation.
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ -cp=/Users/ha/RMI example.hello.Server
This assumes, of course, that the class files are under /Users/ha/RMI/example/hello
Just look inside of the directory to verify that the class files are there. It should be the directory that you designated after the javac command and -d argument.
I used three terminal windows for execution of a rmiregistry program.
1.Use this terminal window to compile all your files (javac filename.java) and use the command "rmiregistry &" this is the windows equivalent command for "start rmiregistry". (Note:'start' command doesn't work on the macOS)
2.Use this to run the java file ie. Server file (ie. java RMI_Server)
3.Use this to run the client file ie. Client file (ie. java RMI_Client 127.0.0.1)
And yeah it worked for me. (Note: The Directory should be set properly in the terminal)
I'm currently running Solr out of a Jetty container that it ships with. It runs correctly when run from the command line via:
java -jar start.jar
when I'm in the same directory as start.jar. Unfortunately I need to be able to launch jetty from any directory, not just the one that contains start.jar. I've tried many options, such as:
java -Dsolr.solr.home=~/solr/ -Djetty.home=~/solr/ -Djetty.logs=~/solr/logs/ -cp ~/solr/start.jar:~/solr/lib/jetty-util-6.1.26-patched-JETTY-1340.jar:~/solr/lib/jetty-6.1.26-patched-JETTY-1340.jar:~/solr/lib/servlet-api-2.5-20081211.jar -jar ~/solr/start.jar ~/solr/etc/jetty.xml
Every time I get this backtrace:
java.lang.ClassNotFoundException: org.mortbay.xml.XmlConfiguration
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at org.mortbay.start.Main.invokeMain(Main.java:179)
at org.mortbay.start.Main.start(Main.java:534)
at org.mortbay.start.Main.start(Main.java:441)
at org.mortbay.start.Main.main(Main.java:119)
Simply changing to the correct directory before calling java.... fixed the problem for me.
Note that when you run
java ... -cp ~/solr/start.jar:... -jar ~/solr/start.jar ~/solr/etc/jetty.xml
the -cp option is ignored since you use the -jar option.
From man java:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
You have two options:
Keep using the -jar option, but then you need to provide the classpath in the jar manifest file (note that these classpath entries can't be relative to the current path, only relative to the jar-file you're executing)
Skip the -jar option and provide the main class explicitly.
You're using the ~ as a short cut to the current user's home directory. I'd replace all tilde characters with an absolute path and see if that helps.
I ran into this in Jan 2014.My issue was that because I ran a Cluster Zookeeper setup from elsewhere, the $SOLR_HOME/lib folder got moved under $SOLR_HOME/cloud-scripts where the zkCli.bat exists.Copied the lib folder back under $SOLR_HOME/ and it works now.