This is the first time I am compiling a program, and it doesn't seem to be working out. Looks like some packages are not being located - so for this question, I'll just focus on one:
Steps I've take so far:
1) setting up the System Variable Path to include java
2) in CMD.exe: jar tf log4j.jar I did this to make sure it includes log4j.Logger and it does.
3) I Shift+rightclick and open command prompt from this folder:
4) Then I enter javac TNT.java and i get the following error (along with others):
Any thoughts?
I set the classpath to the same folders with set classpath = "name of folder" no change...
edit
5) have also tried
javac -cp jdkbindirectory;jrebindirectory;theabovefolder TNT.java
I get this:
blahblahblah
You shouldn't set the classpath using an environment variable as it is bad practice. What if you accidentally change it later for a different project and your current project breaks?
When including classes in the classpath, you can include the path of the root of the package of the class, as in the folder that contains the folders in the package structure. However, when you're including a jar in your classpath, you need to put the entire path of the jar file (relative to the current working directory) all the way up to the jarname.jar.
Also, remember that by default, java looks in the current working directory and uses that as its default classpath. However, as soon as you specify a classpath it no longer does that automatically for you. Be sure that you're including your current directory in your classpath as well.
Finally, be sure to surround the classpath in quotes otherwise java might think its a part of another argument.
I would try this:
javac -cp "./;log4j.jar" TNT.java
And then to execute the class file:
java -cp "./;log4j.jar" TNT
Hope this works, good luck!
I'm using an Apache package; I've add it's jar file location to the classpath environment variable. When I try to compile the code via the command line I get numerous errors including: package org.apache.commons.io does not exist
You are missing the jar that contains this package (in your case apache-commons.jar) on your classpath.
Try to export the packaged jar file with the dependend jars included. That should work.
Complile like this:
javac -cp .:common-io-xx.jar YourProgram.java
Run it like this:
java -cp .:common-io-xx.jar YourProgram
Actually the above option will not work sometimes.
At run time it might not find the class YourProgram :( Strange but true!
In that case just drop common-io-xx.jar under your
...\Java\jdk1\jre\lib\ext
Ofcourse that will work :)
Only wasted a few hours to get to that conclusion!
I know this has come up a number of times, but previous responses just don't seem to help.
My environment variables are :
CLASSPATH C:\Program Files\Java\jre7\lib;C:\Program
Files\Java\jdk1.7.0_15\bin;
PATH C:\Program Files\Java\jdk1.7.0_15\bin;
When moving to the directory as follows C:\Users\Oli\My Documents\java I can compile using javac, but cannot runt he program using java. I know its most likely got something to do with environment variables but I cannot get it to work. P.S the error is "could not find or load main class"
Any help would be appreciated.
CLASSPATH is the place where JRE looks for classes. You've set your CLASSPATH to a value and expect to run the class from current Directory, which won't work.. for instant solution you may use
java -cp C:\Users\Oli\My Documents\java ClassName
Or undo setting CLASSPATH. Default CLASSPATH is current directory
Lets assume that your ".java" file default package ( no package defined) survivies in "C:\Src"
You dont need to set the CLASSPATH in this case.
cd C:\Src
javac MyJava.java
java MyJava
If with package say com.test
cd C:\Src
javac com\test\MyJava.java
java com.test.MyJava
However if you are not in the same folder as Source files and want to run from anywhere
set CLASSPATH=%CLASSPATH%;C:\src
javac MyJava.java or javac com\test\MyJava.java
and
java com.test.MyJava or java com.test.MyJava
Unset CLASSPATH and just use the default one provided by the JVM. Here is a link to the Java Tutorial that covers the environment variables.
Seems like the problem is not in the path...
Does your code use the 'package' statement? (i.e. package my_package;)
If so, go to 'java' directory and execute:
java my_package.MyClass
where 'my_package' is the name of... the package, and MyClass is your compiled .java file (without the .class extension).
Good luck.
I am trying to import a package found at /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar. I am using import ptolemy.plot.* and compiling with javac -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar The Class.java. When I run (using java -cp ...same... TheClass) I get Error:Could not find or create main class TheClass.
When I take away the -cp from the java call the Could not find or create error goes away...
I know this must be something simple but I can't figure this out!
If you specify that the classpath is a single jar files -- as you seem to be doing -- then Java won't find any classes outside of that jar file. You need your classpath to include both the jar file and the location of your compiled classes. You can use "." to mean the current directory; i.e.,
java -cp .:/home/jirwin/ptplot5.8/ptolemy/plot/plot.jar TheClass
Note the "dot colon" prepended to the beginning of the classpath.
You need to put your full package name in front of your .class when you run it with java. Otherwise it looks in the wrong place, or something. (I don't understand java well enough to give you the "why" but it's the idea.)
java -cp /home/jirwin/ptplot5.8/ptolemy/plot/plot.jar {package}.TheClass
I have Windows 7, installed jdk1.7.0 and its supporting jre7.
My problem is compilation part works perfectly, but while running the Java program I get this error saying:
"Could not find or load main class"
I am storing all my programs in javalab folder. I have set the path to it. Procedure looks like this:
C:\Users\user>cd\
C:\>cd javalab
C:\javalab>autoexec.bat
C:\javalab>set path=C:\Program Files\Java\jdk1.7.0\bin
C:\javalab>javac p1.java
C:\javalab>java p1
Error: Could not find or load main class p1
C:\javalab>
I was having a similar issue with my very first java program.
I was issuing this command
java HelloWorld.class
Which resulted in the same error.
Turns out you need to exclude the .class
java HelloWorld
Try:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)
set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause
javac should know where to search for classes. Try this:
javac -cp . p1.java
You shouldn't need to specify classpath. Are you sure the file p1.java exists?
I had almost the same problem, but with the following variation:
I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
when was trying to run the project on PC 2 got the same error "Could not find or load main class"
I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03)
After restarting Ecplise it still didn't work
I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.
That's quite odd behavour, I cannot completely understand it.
Hope It'll help somebody. too.
i guess that you have a different class name in p1.java
Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.
If package is x.y
java x.y.p1
Here is my working env path variables after much troubleshooting
CLASSPATH
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)
bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)
Just a little checklist for people that have this problem still.
I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use
javac -cp . packageName/className
which means if you've had a package named def and main class with name TextFrame.java you'd write
javac -cp . def/TextFrame
omitting the trailing .java extension, and then you run it with the
java def/TextFrame
and if you have have arguments, then you need to supply it with arguments corresponding to your program.
I hope this helps a bit.
First, put your file *.class (e.g Hello.class) into 1 folder (e.g C:\java). Then you try command and type cd /d C:\java. Now you can type "java Hello" !
You might have the CLASSPATH environment variable already added!!
Use following to avoid further usage of -cp . in java -cp . CLASSFILE
Add . to CLASSPATH in system properties->environment variables or by cmd
set CLASSPATH=%CLASSPATH%;.;
I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.
I think this is because it tries by default to run it in some other configuration which causes problems.
Hope this answer helps some.
If you have a single .java file to compile using command-line , then remove
topmost package parts from the code, the compile again, it will work.
This worked for me.
Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:
Could not find or load main class xxx Linux
I just delete that reference and added again and it worked fine again.
i had
':'
in my project name e.g 'HKUSTx:part-2'
renaming it 'HKUSTx-part-2' worked for me
You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours