Question

The problem I'm currently having is that I am trying to execute a java file using command prompt, I understand the PATH being set to the jdk's file. Though my java file contains libraries and has to import the libraries, how would I ' import the libraries ' when it runs?

Sample command : javac ClassName.java 1 1 1

When it executes it errors on the imports so what should I do?

Was it helpful?

Solution

The .jar files are Java "libraries". What you need is something like:

> javac ClassName.java
> java -cp Library1.jar:Library2.jar ClassName.class

The first line (javac) compiles the Java code into a class file. The second line runs the compiled class. The '-cp' option sets the CLASSPATH (makes the code in the jar files available at runtime). Note: the exact syntax will depend on if you are using Mac OSX/Linux or Windows. Windows uses the ';' character to separate the jar file names.

OTHER TIPS

Several issues here:

  1. Are you compiling or executing? javac is the compiler. java is the runtime virtual machine.
  2. ClassName.java is source. ClassName.class is the compiled bytecode run by java.
  3. As a general rule, your environment should contain the variable JAVA_HOME, and that should point at the directory where the JDK resides on your computer.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top