0

I have a project that I have compiled to a jar with Maven which I named H.jar. The maven command I use is (in eclipse):

 maven install

The jar file has a class called Person in it. I have added the jar file to the classpath on a Windows machine.

 echo %classpath% 
 Results -> C:\location_to_jar\H.jar 

But when I try to compile the program I get error:

 error. Cannot find symbol.

I am running the command:

 javac Main.java 

The class looks like this:

 public class Main {
     public static void main(String[] args) {
     Person p = new Person("John", "Doe");
     p.toString();
     }
}

Should the program not just find the class if I have added it to the classpath?

C.A
  • 620
  • 1
  • 11
  • 23
  • Are you importing the `Person` class in the `Main` class? – blalasaadri May 09 '14 at 09:28
  • 1
    When you say "compile", do you actually mean "run"? If the classes are in a jar file, then they have already been compiled. Assuming that you are executing the program, please provide the command line that you are using to perform that execution. – Steve May 09 '14 at 09:31
  • I have tried compiling with import class and without. No difference. It cannot find the symbol. – C.A May 09 '14 at 09:39

1 Answers1

0

It sounds like your program is fully contained within a single jar, with no external dependencies, so I would recommend that you avoid using the %CLASSPATH% environment variable. Instead, point Java at the jar directly.

Assuming that your Maven build has generated an executable jar with your Main class defined as its entry point then you can just execute:

java -jar C:\location_to_jar\H.jar

If the executable class has not been defined in your build, then you can define the classpath on the command line:

java -cp C:\location_to_jar\H.jar com.mypackage.Main

You will need to replace the package in the command above with the appropriate package name as defined in your application.

If you want Maven to make your jar executable, then you can use the Maven Assembly Plugin to help. An answer which explains that can be found here: How can I create an executable JAR with dependencies using Maven?

Community
  • 1
  • 1
Steve
  • 9,270
  • 5
  • 47
  • 61
  • Thanks Steve. I need it for running a Hazelcast cluster. So I need the JVM to be aware of the classes in the jar file. I will try with Maven Assembly plugin. – C.A May 09 '14 at 10:42