我正在努力从我们的网站Java代码创建API供我们的客户使用。我已将项目导出到eclipse中的JAR文件。在一个单独的测试项目中,我能够从JAR中调用某些具有正确功能的方法。
当我启动一个引用javax.servlet.http.HttpRequestServlet的类的新Object(TrackingAction)时,我遇到了问题。我收到以下错误
The import javax.servlet.http.HttpServletRequest cannot be resolved
The import javax.servlet.http.HttpSession cannot be resolved
HttpSession cannot be resolved to a type
HttpSession cannot be resolved to a type
以下是我正在努力工作的代码:
import com.myproject.api.data.ShipmentDetail;
import com.myproject.api.data.ShipmentSummary;
import com.myproject.api.service.ShipmentService;
import com.myproject.web.project.action.TrackingAction;
public class Main{
public static void main(String[] args){
ShipmentSummary shipSum = new ShipmentSummary();
ShipmentDetail shipDet = new ShipmentDetail();
TrackingAction trAction = new TrackingAction(); //ERROR COMES FROM THIS LINE
ShipmentService shipServ = trAction.getShipmentService();
List<ShipmentSummary> lss = shipServ.getShipmentSummaryByProNumber("101844564");
List<ShipmentDetail> lsd = shipServ.getShipmentDetail(lss.get(0));
shipDet = lsd.get(0);
shipSum = lss.get(0);
System.out.println("Summary status: " + shipSum.getStatus());
System.out.println("Detail pickup: " + shipDet.getPickupNumber());
}
}
我到处都看,我找不到合适的解决方案。我尝试过使用ClassLoaders但没有成功。我尝试过OneJar,它似乎没有正常工作(除非我做错了)。
答案 0 :(得分:0)
您正在导入Enterprise Java(Servlets等)的类(来自您的API类)/即使项目本身不是SE,使用main()方法中的那些是针对SE执行的错误。
Java SE项目没有:
httpsession,servlet等可用的唯一方法是在带有tomcat(或其他一些servlet容器)的servlet中运行此代码。
将您的课程重构为: