setting the correct classpath for compiling and running Java packages? [duplicate]

主宰稳场 提交于 2019-12-02 19:17:51

问题


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


来源:https://stackoverflow.com/questions/21711020/setting-the-correct-classpath-for-compiling-and-running-java-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!