setting the correct classpath for compiling and ru

2019-03-05 06:17发布

问题:

This question is an exact duplicate of:

  • getting error when I compile the Java code using package in commandline? 1 answer

I have been using Eclipse lately, where compiling and running the program is very simple. Not much needs to be done in setting the classpath. But apparently that is not the case when it comes to running them from commandLine. when I try compiling from terminal, I am having various errors. I am pasting an image of my package structure of the project cp125_soln. Now I want to compile Invoice.Java in the com.scg.domain package, I tried

javac src/main/java/com/scg/domain/Invoice.java 
src/main/java/com/scg/domain/Invoice.java:17: error: package com.scg.util does not exist
import com.scg.util.StateCode;
.......................//long error message

This means I do not have com.scg.util.* in my classpath. so I tried

javac -cp src/main/java/com/scg/util/*  src/main/java/com/scg/domain/Invoice.java 
src/main/java/com/scg/util/ListFactory.java:8: error: package org.slf4j does not exist
import org.slf4j.Logger;
                ^
src/main/java/com/scg/util/ListFactory.java:9: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
                ^
src/main/java/com/scg/util/ListFactory.java:11: error: cannot find symbol
import com.scg.domain.ClientAccount;
                     ^
  symbol:   class ClientAccount
  location: package com.scg.domain
  ................... // long error message

I read different articles on how classpath works and how to provide it in command-line. but when it comes topackage level structures, I am not able to find a good tutorial on how to COMPILE and RUN packages. If a little help could be provided here on the propery way to compile and run these kind of packages, it will be very helpful.

回答1:

javac src/main/java/com/scg/domain/Invoice.java

Try this:

cd src/main/java
javac com/scg/domain/Invoice.java