Are the imported packages included in the .class file after compilation?

Packages that are not included in the JDK (for example

javax.websocket

) need to be downloaded and referred to with

-cp

or the

CLASSPATH

environment variable in order to be imported (otherwise you get the compilation error package X does not exist).

After compilation, when the .class files are obtained, is it possible to transfer these files to a computer that does not have the

javax.websocket

package, and have the JVM on that computer run them, or is it necessary to have the package on both computers?

In other words: When you write

import bar.foo;

, you are essentially writing "Every time you see

foo

, go to this location and see what it is.". However, when you have compiled the source code, does the byte code still say "go to this location and see what it is", or has it already gone to the location and "imported" everything so that the location is no longer needed?

After compilation, when the .class files are obtained, is it possible to transfer these files to a computer that does not have the javax.websocket package, and have the JVM on that computer run them, or is it necessary to have the package on both computers?

It is necessary to have the package on both computers. Bytecode assumes that the relevant classes would be made available to JVM at runtime.

Moreover, this is true even for compiling and running on the same computer: the locations from which Java compiler pulls its packages for compilation could be different from the location from which JVM pulls packages when running your code.