This is first of my question .I have got two classes and an interface .i am not able to compile the following code.Can anyone please help we out why the code is not compiling and running.
public interface Animals {
public void run();
public void eat();
public void excreate();
public void mate();
}
public abstract class Mammals implements Animals {
public void run() {
System.out.println("Run with Four Legs");
}
public void eat() {
System.out.println("Eat Grass");
}
public void excreate() {
System.out.println("Excreate Excreata");
}
public void mate() {
System.out.println("Mammals Mate with a Mammal");
}
public abstract void giveBirth();
}
public class Horse extends Mammals {
public void mate() {
System.out.println("Horse Mates With A Mare");
}
public void giveBirth() {
System.out.println("Giving Birth To Foal");
}
public static void main(String[] args) {
Horse h1 = new Horse();
h1.eat();
h1.run();
h1.mate();
h1.giveBirth();
}
}
The Animal code Compiles successfully while the mammals and Horse class is not compiling .
The error is attached in the screen shot below
scrren1
The issue is that you're compiling files one by one, and javac can't find Animals when compiling Mammals. From the javac docs,
When compiling a source file, the compiler often needs information about a type whose definition did not appear in the source files given on the command line.
When the compiler needs type information, it looks for a source file or class file which defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath command line option. (For details, see Setting the Class Path).
If you set the -sourcepath option, the compiler searches the indicated path for source files; otherwise the compiler searches the user class path for both class files and source files.
As you can see from your screenshot, the directory you're compiling in isn't being searched for source or class files. javac is effectively unaware of what Animals is.
You can either set the source path/class path to contain your source folder or directly compile multiple source files, as the javac docs show:
Compiling Multiple Source Files
This example compiles all the source files in the package greetings.
C:\>dir /B
greetings
C:\>dir greetings /B
Aloha.java
GutenTag.java
Hello.java
Hi.java
C:\>javac greetings\*.java
C:\>dir greetings /B
Aloha.class
Aloha.java
GutenTag.class
GutenTag.java
Hello.class
Hello.java
Hi.class
Hi.java
Related
Here My first file code
package com.shubham.packages.a;
import static com.shubham.packages.b.Message.Hello;
public class Greeting {
public static void main(String[] args) {
System.out.println("Hello World");
Hello();
}
}
Here my second file code
package com.shubham.packages.b;
public class Message {
public static void main(String[] args) {
}
public static void Hello() {
System.out.println("This is Awesome.");
}
}
Here the error I got when I run the program.
When you compile your code javac needs to know where to look for all of your source/class files. You could go to "java_tutorial" folder and run.
javac com/shubham/packages/a/Greeting.java com/shubham/packages/b/Message.java
That should compile both of your java files into class files at the same location. Then you should be able to run.
java com.shubham.packages.a.Greeting
I might be using the wrong slashes for windows
You can explicitly name all of the necessary java files, so this should cause both Greeting.java and Message.java to be compiled in place.
When you run java, the CWD is on the classpath by default so that means the package com.shubham.packages.a and ...b should be on the classpath in their correct location.
A slightly better way to do this is to create a folder called "build" or whatever you like.
javac -d build com/shubham/packages/a/Greeting.java com/shubham/packages/b/Message.java
That will output the class files to the build folder. Then when you run it.
java -cp build com.shubham.packages.a.Greeting
I have classes A and C in package abc. A has a static method showA(). Now I want to use this method in C.How do I do this?
package abc;
public class A{
public void static showA()
System.out.println("I am in A");
}
}
package abc;
public class C{
public void static showC(){
A.showA();
System.out.println("I am in C");
}
}
Now while compiling C it shows that, cannot find variable A.
How to resolve this?
You didn't give exact information about what you did, but I fear that you are compiling the classes one by one with calls like
javac abc/A.java
javac abc/B.java
You have 2 possibilities:
The first one is to tell the compiler to compile both classes. That way both classes will be known:
javac abc/A.java abc/B.java
Another possibility is to tell the compiler where the required class file can be found. As A.Java is compiled to A.class with the same base directory, you could do the calls:
javac abc/A.java
javac -cp . abc/B.java
With -cp you add the local directory to the classpath so A.class is on the classpath.
I've the following two source files
File World.java
package planets;
public class World {
public static void main(String[] args) {
Mars.land();
}
}
File Moon.java
package planets;
public class Moon {
public static void land() {
System.out.println("Hello Moon");
}
}
class Mars {
public static void land() {
System.out.println("Hello Mars");
}
}
As we can see, the Moon.java contains two classes: the public Moon class and the nonpublic Mars class.
The files are located inside planets directory, below is showed the directory tree
+current-dir:
+----+planets:
+----+World.java
+----+Moon.java
Now, if I try to compile from Windows command prompt (I'm inside current-dir folder) typing
javac planets\World.java
I receive this error message:
planets\World.java:5: error: cannot find symbol
Mars.land();
^
symbol: variable Mars
location: class World
1 error
It's very strange, because I know that the compiler searches for nonpublic classes inside all the source files of the current package.
Also Cay Horstmann's Core Java Vol 1, 10th ed. at pp. 192-193 says that:
[...]you can import nonpublic classes from the current package. These
classes may be defined in source files with different names. If you
import a class from the current package, the compiler searches all
source files of the current package to see which one defines the
class.
In addition I tried to write these files using Eclipse Oxygen and it compile without problems. But I know that Eclipse use a different compiler.
Why does javac compiler fail?
EDIT: I have not set CLASSPATH variable. So by default compiler looks inside current directory.
you need to type the following commands in order (inside your 'current-dir')
javac planets\Moon.java
javac -cp . planets\World.java
java -cp . planets.World
I'm newer in Java Card Platform so please be patient with me. I'm trying to develop an RMI application for the Java Card 3 Platform. My IDE is Eclipse and my OS is Windows 10. I start by creating a simple interface IContor.java responsible for increasing, decreasing, etc. certain values.
Here is my interface:
package sid;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javacard.framework.UserException;
public interface IContor extends Remote{
public void Incrementer()throws RemoteException,UserException;
public void Decrementer()throws RemoteException,UserException;
public byte GetValue()throws RemoteException,UserException;
public void Init(byte value)throws RemoteException,UserException;
}
Then I provide an implementation for this interface which I named Contor.java:
package sid;
import java.rmi.RemoteException;
import javacard.framework.UserException;
import javacard.framework.service.CardRemoteObject;
public class Contor extends CardRemoteObject implements IContor {
private byte contor = 0;
#Override
public void Incrementer() throws RemoteException, UserException {
++contor;
}
#Override
public void Decrementer() throws RemoteException, UserException {
--contor;
}
#Override
public byte GetValue() throws RemoteException, UserException {
return contor;
}
#Override
public void Init(byte value) throws RemoteException, UserException {
contor = value;
}
}
My Test applet worked ok. Below I wrote that peace of code:
package sid;
import javacard.framework.*;
import javacard.framework.service.Dispatcher;
import javacard.framework.service.RMIService;
public class Test extends Applet {
Dispatcher dispatcher;
public static void install(byte[] bArray, short bOffset, byte bLength) {
new Test().register();
}
protected Test() {
RMIService rmiService = new RMIService(new Contor());
dispatcher = new Dispatcher((short)1);
dispatcher.addService(rmiService,Dispatcher.PROCESS_COMMAND);
}
#Override
public void process(APDU apdu) {
dispatcher.process(apdu);
}
}
This is a standard piece of code. However I want to create a client which uses that interface which implements the interface Remote. So I create a Java application in which I copied the IContor.java interface. Then I opened a command prompt and do the following things:
Go into the directory where the source files of the first project is located (cd bla_bla/Contor/src)
Go a directory up (cd ..)
Go into bin directory (cd bin)
Here I have located the name of the package ( sid) and into package sid I have those three files (Contor.class, IContor.class and Test.class).
Then I typed the following command on the command prompt:
rmic -v1.2 -classpath .;%JC_CLASSIC_HOME%lib/tools.jar -sid/Contor
but I got the following error:
Class javacard.framework.service.CardRemoteObject not found in class sid.Contor.
I replace the tools.jar with api_classic.jar but I still get the same error .
The %JC_CLASSIC_HOME% contains the path to the Java Card 3 development kit. tools.jar contains compiled implementations of packages javacard.framework, javacard.security, javacardx.biometry, javacardx.external and javacardx.framework.tlv . My bout is to generate a client application in bin/sid directory.My %JC_CLASSIC_HOME% value is C:\Program Files (x86)\Oracle\Java Card Development Kit 3.0.5ga\ and I'm using JDK 1.8
Here is my Package Explorer from Eclipse :
What does the error "Class javacard.framework.service.CardRemoteObject not found in class sid.Contor." mean?
This means that you reference the class CardRemoteObject from within your class Contor (as Contor extends CardRemoteObject). However, the class path that you use to compile the RMI stub does not contain this class.
The class javacard.framework.service.CardRemoteObject is located in lib/api_classic.jar. Hence, the correct class path for the Java Card Classic API (which is what you need to generate the stub class for RMI) is lib/api_classic.jar. lib/tools.jar is the Java archive that you later need to compile your client applications against. lib/tools.jar only contains the Java Card related exception classes. However, for compiling the remote interface stub you need the whole Java Card API (at least those classes that are referenced from your interface classes).
How does -classpath work?
The parameter -classpath is a list of directories and/or Java archives (.jar files) that contain all the relevant classes to compile a given Java source file (for javac, the Java compiler) or to compile an RMI interface stub from a given Java class file (for rmic, the Java RMI compiler).
For instance, the parameter -classpath .;"%JC_CLASSIC_HOME%/lib/api_classic.jar" specifies two paths (multiple paths are separated by semicolons (";")):
. specifies the current directory (in your case the bin directory).
"%JC_CLASSIC_HOME%/lib/api_classic.jar" specifiees the Java archive api_classic.jar from your Java Card Development Kit.
Within those paths, classes are organized in directories mapping to the components of the Java package names (e.g. a class sid.Contor would be located in a file sid/Contor.class; a class javacard.framework.service.CardRemoteObject would be located in a file javacard/framework/service/CardRemoteObject.class).
How to compile the RMI stub?
Goto your src directory.
Compile the Java classes of the applet. In your case, you could use something like:
javac -classpath .;"%JC_CLASSIC_HOME%/lib/api_classic.jar" -d ../bin sid/IContor.java sid/Contor.java sid/Test.java
Goto your bin directory: cd ../bin
Compile the Java RMI stub. In your case, you could use something like:
rmic -v1.2 -classpath .;"%JC_CLASSIC_HOME%/lib/api_classic.jar" sid.Contor
The class must be specified without a trailing dash ("-") and using dots (".") instead of slashes ("/"). If the value of the environment variable JC_CLASSIC_HOME contains spaces, you need to surround the whole path of the api_classic.jar file with quotation marks.
I have successfully compiled this file and saved.
class A
{
public static void main(String[] args)
{
System.out.println("Hey!");
}
}
using
javac a.java
java A
but when I compile this file:
package B;
class A
{
public static void main(String[] args)
{
System.out.println("Hey!");
}
}
now, again using the same commands it do compile but never run
javac a.java
java A
// could not find or load main class
Please guide me the exact command for the terminal to run the file.
Note: The file is named "a.java".
You need to specify the fully-qualified name, i.e. packageName.ClassName:
java B.A
you have to change the directory to the directory wich contains the path 'B' (your package name) and than call java B.A
As mentioned above, You need to specify the fully-qualified name, i.e. packageName.ClassName:
>> javac a.java
>> java B.A
But you need to create the directory named "packageName" yourself as jdk does not create one for you implicitly.