user1122000 user1122000 - 1 year ago 73
Java Question

compiling and running java code on cmd prompt with .jar files

I have some code on a Windows Machine placed in the following directories.

folder (parent folder) containing:

1. subfolder1:
3. subfolder2:

There is also a
folder containing:

1. x.jar
2. y.jar

This code was originally written on Eclipse and therefore compiling and run it was straightforward. Here I guess there are many things I need to consider.


  1. imports subfolder1.c

  2., and are all in the same package

  3., and are all in the same package and import
    libraries from x.jar and y.jar in the lib folder.

How I am trying to compile:

javac -cp .:../lib/*

I get: error: package subfolder1 does not exsit.

For some reason it won't notice the file when compiling the

If I get it to compile would running it be as simple as:

java runfile

or do the .jar files have to be mentioned somehow as well?

Answer Source

If your project gets more complex I'd recommend using a proper build tool like (in best-to-worst order) Gradle, Ant or Maven.

javac -sourcepath . -cp ../lib/*

should work for compilation I think and yes, you need to set the classpath for runtime also like

java -cp .;../lib/* runfile
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download