Program compiling in Eclipse but not in Command Prompt - java

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.

Related

Unable to compile, classpath not set, package does not exist?

Normally I can use javac to compile and java to run perfectly fine, they are set correctly into my computer's classpath. In this instance however, one of my files Graph.java is trying to import a jar file in the same directory Heap.jar.
When I try to use javac *.java, it tells me package mycs1 does not exist.
Normally in my lab on Linux machines, we can use export CLASSPATH=mycs1.jar:. and then javac will work normally again, and just add more files if there are more jars.
My problem is that this command does not work on my home computer running Windows 10. I am trying to set the classpath of multiple jars. This is similar to this question:
Unable to execute jar file despite having PATH and CLASSPATH set
But despite reading that, I still cannot find a way to set the path of multiple jar files in such a way that javac will work.
Is this possible? If not, what alternative command can I use?

What are Java Classpath and Path?

I have been googling around, trying to understand what the Java Classpath and Path are. However, I am stil not quite sure if I have understood it or not. If feel that this topic is one of those grey areas.
Can someone explain me what those are? I mean, where do I find and set them (where is the actual text file on Mac/Windows)? Is there only one instance of each one? If so, how do I set the path for multiple classes?
As you might have notices, I am totally confused right now after reading so many different tutorials... So now I really would like to have a straight forward explanation.
Please help me, I just trying to learn :)
Thank you all
A path is just a folder location. The path is where your OS will look for programs by default. If java, javac, javap, etc, etc, are in your path then you can just type their names without the entire folder location.
Your classpath is similar. It is a set of folders that contain .class files describing classes(hence the name) and .jar files, which are basically files that contain .class files. All code that you're running is either out of the classpath, generated, or out of the java libaries(also part of the classpath, techncically).
With each run of a java program you can specify a classpath by parameters passed to the java executable. It also grabs classes out of "extension folders,", special folders Java keeps around to act as a system classpath, and finally, the "bootstrap classes", which are a set of important classes almost any Java program needs to run.
Simple mean of path is location of file system. if you want to access any file then you have to manually needs to go there location.
just example: d:\text1.txt then needs to go that d:\ location. same way java program have command like
javac -for compile
java - for run
.
.
.
etc.
that inside java-jdk\bin folder
so if you don't set into classpath. then you can execute java program like
run->cmd
c:\jdk1.6\bin> javac test.java
so without going explicit way you can set it into classpath, and direct execute java program from anywhere.
You can set java path as environment variable of computer.
The PATH is basically where your JDK is installed; this is essentially what your IDE will look for when trying to compile or create Javadoc or such; it's basically just the location of a folder on your hard drive, set as a Windows (or other OS) environment variable to make it easier to use.
The CLASSPATH is a property that tells the compiler where to look for classes. Basically if you download a library or such from somewhere, you need to add it to the CLASSPATH for the compiler to use it. Usually you can do this in your IDE, however, you should not need to directly access the CLASSPATH variable.
By the way, the Wikipedia article is pretty helpful.
1)java Path: it is location of binary executable files
example :javac , java
this file are used for compile and run
2)class Path: it is location of .class file(file create after compile your source code .java file)

How to compile servlets from command prompt?

I'd like to compile a very basic servlet from command prompt, but it is always unsuccessful and the compiler tells me the following:
error: package javax.servlet does not exist.
I googled for the solution and I found that I need to include the servlet.jar libraries into my PATH.
I believe I did.
I strongly believe that the location for those libraries in my computer is:
C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
and the end (the relevant part) of my PATH is the following:
%JAVA_HOME%\bin;C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
For me, it looks ok, but it is obviously not. Can anyone tell me what could be the problem?
classpath not path ... and you don't need it as an evironment variable.
You can set the classpath for javac with option -cp or -classpath (several other ways are also available).
javac uses the environment variable CLASSPATH to look for classes, that can be useful and can also be a source for hard-to-track-down-problems.
An example to compile a java file that uses a library(that is classes from outside the standard JavaSE) would be:
javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
if your environmental variable CLASSPATH contains libraries you need you might want to do:
javac -classpath %CLASSPATH%;C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
(please be aware that I don't have access to a windows machine, and therefore haven't tested the idiosyncratic parts of the syntax above)
(also note that in this example "C:\apache-tomcat-7.0.23\lib\servlet-api.jar" is a jar file and not a directory which it might be from your question on your machine)
For command line compiling on windows OS it is a good idea to have the environmental variable JAVA_HOME set correctly and the bin directory of the JDK in the PATH.
I do however suggest getting to write-compile-execute-deploy via/in an IDE for servlet development before figuring out how to do it with just the JDK from a command line.
Java Servlets are not stand-alone executable classes but needs to be deployed into for example tomcat to be tested/used.
First copy the servlet-api.jar file from following path
C:\apache-tomcat-7.0.23\lib\servlet-api.jar;
and paste it in the bin folder of java software by following the path
C:\java\jdk1.6\bin;
Hope now you can successfully compile your servlet program.
1.You can copy your javax.servlet.jar in the jdk1.6\lib folder.
2.You can go to Control Panel\System\Advanced System Properties\Environment Variables
Enter the location of the jar in the CLASSPATH variable as follows:
Then compile and run the servlet.

Compiling Java package throws errors for external Jars

Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )

Again "wrong name" error when executing java program

With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.

Categories