user1122000 user1122000 - 12 days ago 6
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.

src
folder (parent folder) containing:


1. subfolder1:
a.java
b.java
c.java
3. subfolder2:
d.java
e.java
f.java
4. runfile.java


There is also a
lib
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.

Dependencies:



  1. runfile.java imports subfolder1.c

  2. a.java, b.java and c.java are all in the same package

  3. d.java, e.java and f.java 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/* runfile.java


I get: error: package subfolder1 does not exsit.

For some reason it won't notice the c.java file when compiling the runfile.java

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

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/* runfile.java

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

java -cp .;../lib/* runfile
Comments