I am having a doubt.
My understanding is that jdk has [ jre + development tools (Java, javac, debugger etc.) + source code (src.zip) ].
Now working of java compiler is nothing to do with the running of class file.
If I am compiling a .java file then from where the java compiler is importing the package?
I could find the packages under jre.
If I do not opt to install jre while installing jdk, does that mean I will not be able to compile the java file having import statement?
Please help.
First, as a minor remark, a statement like
import java.util.List;
just introduces an abbreviation, allowing you to use the simple word List later in your code instead of the full class name java.util.List. So it's not so much the import statement itself, but the usage of a class like java.util.List that needs some explanation.
You understand correctly that, to compile your java file, the compiler needs some information about every class you use, and it typically finds this information in some jar file containing that class.
Now, where is this jar file containing the java.util.List class that the compiler reads? You're correct, it comes from the JRE, from the rt.jar that's part of the system classpath (the Java compiler itself is a java program that needs the basic classes itself, so wherever you successfully run javac, you always have an rt.jar available).
If your source code used a class from some other library, you'd have to specify that library on the javac command line, using an option like -cp.
Jdk = JRE + other tools like you mentioned.
When you are compiling your java file and you are using java inbuild library then it uses rt.jar to resolve dependency i.e import statements.
You can refer below link for the difference
What is the difference between JVM, JDK, JRE & OpenJDK?
Related
I understand that I need to import the packages in
import com.ericsson.otp.erlang.*;
To run Jinterface in Java, this is not included in Java's default libraries but in Erlang's. How do I access this library? Which path should I use? I've google it but found nothing. I am using Ubuntu 13.10. The above code is not enough for this to work.
If you have done any Java development before, then you know that you should add OtpErlang.jar to your application's class path.
You can do this in the command line, Ant, Maven, Gradle or even in your IDE.
Command line example:
javac -classpath OtpErlang.jar YourGame.java
I use OS X and OtpErlang.jar is under:
/usr/local/Cellar/erlang/R16B03-1/lib/erlang/lib/jinterface-1.5.8/priv/OtpErlang.jar
Keep in mind that you need to include OtpErlang.jar also when you run your game.
Try looking in the Jinterface users Guide, they give an example of compiling the Java code.
Maybe this question may be splitting hairs, but when I compile a file from command line like :
javac MyClass.java
then afterward I cannot run it by saying
java MyClass.class
I have to call:
java MyClass
What is the motivation for this notation?
Because you run a class on the classpath, which may be contained inside a jar for example. You couldn't use your syntax in that case.
Java compiler needs a compilation unit; this is by default (at least) a java source file, with the whole of classes defined in it and its dependencies.
Java interpreter (the jvm) needs a single class with a main method as entry point of the execution - it must start somewhere.
You'd have to ask Sun (now Oracle) for the development history, but I do want to point out that for folks who are just using Java rather than developing Java, "java DoSomething" is easier to remember, and to type, than "java DoSomething.class"
There is no way to run a Java program that is not a class. For that reason, there is no reason to mandate typing the ".class". You might also invoke a class from within a JAR on your path, or directly, but it's still instantiating a class (possibly a "default" class from the Manifest).
Because the name of the class is MyClass and not MyClass.class. And when running java you specify the CLASS NAME and not the PATH to the actual compiled file.
For more in depth knowledge I guess Sun & Oracle will have to answer :)
Imagine that you have a class named package and you have a class named Class, in a package named package,
--CurrentFolder
--package
Class.class
package.class
so executing java package.class may lead to an undecidability to the compiler!
I have two .class files that I'm supposed to black box test. These are in a package one.two.three. My tests are also in the same package. There is a third .class file in the same package whose purpose is to hold an enum variable for the Orders class I'm supposed to test. In eclipse, I'm able to get the junit tests for Orders to work by importing the enum directly e.g.
import one.two.three.Orders.ShippingMethod;
If I try to do this using Ant or via the command line, I get the error "package one.two.three.Orders does not exist". If I change the import statement to
import one.two.three.*;
Ant, Eclipse, and the terminal cannot find any of the classes I have. I need to compile and run the test cases with Ant. The classes are in bin/one/two/three Any help would be greatly appreciated, thanks.
Import Orders, as it is the class, and assuming that ShippingMethod is an enum within that class, the correct way to reference its type is Orders.ShippingMethod.
Attempting to import a class's internal types sometimes works oddly in Eclipse. This is likely due to Eclipse not using the javac compiler packaged in your jdk, while Ant does (it has to, because Ant doesn't ship an embedded compiler).
import one.two.three.Orders;
public class Whatever {
private Orders.ShippingMethod shipMethod;
}
This should work in everything, as it's the right way to do it.
import one.two.three.Orders.ShippingMethod;
could easily confuse most compilers as there is not a
one/two/three/Orders/ShippingMethod.class
file, which means the class loader won't find it at runtime.
I'll bet it's a bug in the Eclipse embedded compiler, as I've seen quite a few. On the bright side, the Eclipse embedded compiler exists to provide faster, tighter integration between code editing and Eclipse. On the dark side, that means that sometimes the Eclipse compiler and the javac compiler differ. When in doubt, the javac compiler is probably correct.
You'll need to set the classpath.
I don't know exactly on Eclipse (I use NetBeans), but I click on Libraries -> add JAR/Folder.
For command line, you need to specify class path
java -cp path/to/my/files (...)
I found some classes designed for debugging in package com.sun.jdi like VirtualMachine, but I can't use this because package seems not exist in Sun JDK7.
How to use this package?
BTW. lib/sa-jdi.jar isn't the same I want
According to this page, the VirtualMachine class that you linked to is part of the tools.jar file which is only distributed in a JDK (not a JRE). It says ...
"Update Note 2: The Attach API is in tools.jar, so you will need to add /lib/tools.jar in your CLASSPATH to compile and run the example on JDK 6."
... and the same advice would apply on (at least) JDK 7 as well.
I have a java program that I'm required to compile into a Linux native program using gcj-4.3. This program requires serial port access. The javax.comm api provides serial port access but I'm not sure how to get my compiled java program to use it.
The target box has Java installed, but of course my compiled program isn't running in the JRE...so I'm not exactly sure how I can link in the comm.jar file or how that file can find the .properties file it requires.
I wonder if I can just compile the comm.jar allong with my .jar file and link the two object files together. Can my code then reference the classes in comm.jar?
Thanks in advance for your help!
I'm not an GCJ expert but I have some suggestions (I'm not providing the syntax, this will require some exploration that I didn't perform):
first, I think that you'll have to compile comm.jar into a (shared) library,
then, you'll have to link your code against the library,
finally, use the GCJ_PROPERTIES environment variable to pass properties to the program at invocation time.
The following pointers might be helpful to implement this:
GCJ---The GNU Compiler for Java (great resources IMO, covers all the steps mentioned above)
GCJ – Getting Started (more an intro but still nice)
Compile ActiveMQ with GCJ (more use cases but I don't think they apply here)
And of course, the official documentation :)