0

I have depended on using IDEs all the time for Java and would like to use the terminal to understand more.

I have a Java application called test.java. It depends on other jar files to compile and run.

The first line of my application also creates a package as follows

package package1;

Now, when I compile this with :

javac -cp .:"JAR FILE PATHS HERE" test.java

It compiles fine. However when I try to execute it as follows:

java -cp .:"JAR FILE PATHS HERE" test

I get the error Error: Could not find or load main class test

If I don't create a package in my application with package package1; , it executes fine.

How do I execute it if I do create package1 tho? using path package1/test doesn't work

Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • See also: https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line – KevinO Mar 04 '19 at 18:36

1 Answers1

1

You need to double check your folder structure- as you know, when using folders directly (instead of jar files), packages are subfolders under the classpath (see the complete doc, or read below for an example).

That means, in your example you should have the folder structure:

workingFolder
    \- package1
        \- Test.java
        \- Test.class

From workingFolder, you run javac package1/Test.java ; that produces Test.class under package1.

To run, from workingFolder you run java -cp . package1.Test.

The folder workingFolder is in the classpath, so package1.Test is resolved as package1/Test.class; the package1 folder is basically the package1 package.

edit and the Test class must be in the right package, like:

package package1;
class Test {
  public static void main(String[] v) {
    System.out.println("hw!");
  }
}

Also, see https://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html for a good explanation on how to compile and run a Java program using the command line.

Daniele
  • 2,672
  • 1
  • 14
  • 20
  • I'm not sure I understand. "subfolders under the classpath" . My classpath is empty. My jar files are in the same directory as my java file , and so when i compile i add them like for example javac -cp .:jar1.jar:jar2.jar test.java. Then i execute with java -cp .:jar1.jar:jar2.jar test . Even if i create a subfolder called package1, which is the name of my declared package inside the java file, and put everything inside this folder, it still doesn't find the class file to execute – Engineer999 Mar 04 '19 at 22:51
  • With `-cp .` , the current folder (`.`) is included in the classpath, and its contents belongs to the classpath also. For jar files, you can simply unzip them, and packages are subfolders within the jar (zip file). ..well double check that the test class has `package test;` on top of it – Daniele Mar 04 '19 at 23:13
  • There is no package named test . This is what confuses me. My package name is package1 – Engineer999 Mar 05 '19 at 02:01
  • @Engineer999 well, I used the name `test` as package name; the whole explanation works the same if we change package name. Anyway, I have edited the answer so that `package1` is used instead- – Daniele Mar 05 '19 at 09:41
  • Thanks. Does this only work if the required JAR files are extracted , and on the same level as the application, then adding -cp . ? – Engineer999 Mar 05 '19 at 10:59
  • it also works if jar-s are not extracted, so that you could compose the classpath with multiple jars and folders, like in `-cp folder1:folder2:jar1:jar2` etc. (where any entry is either the absolute or relative path of a folder or jar, starting from your current directory) – Daniele Mar 05 '19 at 11:06