I am working on creating an API from our website Java code for our customers to use. I have exported the project to a JAR file in eclipse. In a separate test project, I am able to call certain methods from the JAR with correct functionality.
I am running into issues when I initiate a new Object of a class (TrackingAction) that references javax.servlet.http.HttpRequestServlet. I get the following errors
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
Here is the code I'm trying to get to work:
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());
}
}
I've looked all over the place and I can't find a working solution. I've tried using ClassLoaders with no success. I've tried OneJar, and it doesn't seem to work correctly (unless I'm doing something wrong).
You are importing classes (from your API classes) that are Enterprise Java (Servlets etc)/ Even if the project itself is not SE, it is wrong to use those from a main() method which is aimed for SE execution.
Java SE projects do not have:
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpSession
The only way httpsession, servlet, etc will be available is when your running this code in a servlet with tomcat (or some other servlet container).
Refactor your classes to:
separate your core API logic from Java EE servlet dependent code
rewrite your servlet methods (e.g. doGet() ) to also reference and use this API.
create a JAR with only your API logic, which you be referenced by your Java SE application and Java Servlet.
Related
I have built a jar from my Scala project.
I have the following structure for what I want to use from this jar
package aaa.bbb.ccc
case class FooResult(...)
trait Foo(...) {
def bar(): FooResult
}
object Foo {
private class FooImpl(...) extends Foo {
...
}
def apply(...): Foo
}
First question: Maybe I have misunderstood something in what Py4J offers,
but do I have to write a java/scala class to start the Py4J gateway if I want to use my own classes? Or is it enough to add it to the gateway's jvm's classpath?
Second question (which I guess doesn't apply depending on the answer to above): How do I add my jar when starting the java gateway in order to make it available? To solve this temporarily, I just started the jvm manually with my jar along with the Py4J jar with this command
java -classpath "path/to/py4j.jar:path/to/my.jar" py4j.GatewayServer 0
and then connected to it manually from the Python code. Then I tried to import my classes via
java_import(gateway.jvm, "aaa.bbb.ccc.*")
which didn't throw any error but I'm not sure it worked because it doesn't throw any error if I input some fake classpath.
Third question (which applies if the answer to the first is that I have to write the entry point to access my classes): How does this work when using scala?
object Main extends App {
val gw = new GatewayServer(// TODO: how to expose my classes here?
}
I am attempting to build my first Java web service using this tutorial http://www.ibm.com/developerworks/webservices/tutorials/ws-jax/ws-jax.html but it produces errors at a certain point that I cannot resolve. The tutorial includes a download and even when I simply use the relevant file from the download its still gives me the errors. All java classes have complied until this point. The OrderProcessService class has complied fine and I have checked all spelling of files and folder names but still it is as if the java compiler cannot see the OrderProcessService class. What am I doing wrong here? I have copied in the OrderProcessService class and the OrderWebServicePublisher class. The other classes in the bean directory, such as Customer and Address are just POJO's. Here is the error;
The OrderProcessService.java
package com.ibm.jaxws.tutorial.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.ibm.jaxws.tutorial.service.bean.OrderBean;
// JWS annotation that specifies that the portType name of the
// Web service is "OrderProcessPort" the service name is
// "OrderProcess" and the targetNamespace used in the generated
// WSDL is "http://jaxws.ibm.tutorial/jaxws/orderprocess"
#WebService(serviceName = "OrderProcess",
portName = "OrderProcessPort",
targetNamespace = "http://jaxws.ibm.tutorial/jaxws/orderprocess")
// JWS annotation that specifies the mapping of the service onto the
// SOAP message protocol. In particular, it specifies that the SOAP
// messages
// are document literal
#SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class OrderProcessService{
#WebMethod
public OrderBean processOrder(OrderBean orderBean){
// Do processing
System.out.println("processOrder called for customer"
+ orderBean.getCustomer().getCustomerId());
// Items ordered are
if(orderBean.getOrderItems() != null) {
System.out.println("Number of items is"
+ orderBean.getOrderItems().length);
}
// Process Order
// Set the order ID
orderBean.setOrderId("A1234");
return orderBean;
}
}
The OrderWebServicePublisher.java
package com.ibm.jaxws.tutorial.service.publish;
import javax.xml.ws.Endpoint;
import com.ibm.jaxws.tutorial.service.OrderProcessService;
public class OrderWebServicePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/OrderProcessWeb/orderprocess",
new OrderProcessService());
System.out.println("The web service is published at
http://localhost:8080/OrderProcessWeb/orderprocess");
System.out.println("To stop running the web service , terminate the
java process");
}
}
Looks like you are running from the command line. You will need to specify the classpath of all required classes.
Instead of doing
javac com\....\OrderWebService.java
do
javac -cp <path to your OrderProcessorService> com\...\OrderWebService.Java
More examples please see Setting multiple jars in java classpath
I am going to guess you haven't setup the classpath correctly? If running from command line utilize the -cp option. If running from IDE, define accordingly.
Did you write this in an IDE or a text editor. An IDE like Eclipse would have caught it. But usually this error is seen in situation like a jar is missing in your classpath. But it seems you have the java file and compiled it again. For your case when I looked at the link in your question : did you run:
wsgen -cp . com.ibm.jaxws.tutorial.service.OrderProcessService -wsdl ?
I have read that Nashorn supports some options but it seems all of them are undocumented.
They can be used by setting system properties but they are global. I want to set them programmatically for a single engine instance.
Here you can read it is possible to specify options the way I want but that classes are internal so I get this kind of error:
"Access restriction: The type NashornScriptEngineFactory is not accessible due to restriction on required library C:\Program Files\Java\jdk1.8.0\jre\lib\ext\nashorn.jar".
I can't find a way to set those options with the java scripting API.
EDIT:
It seems that the problem is eclipse protecting me from using internal classes and not exactly a problem using nashorn but I will concrete the question.
Is there a way to setup the options of a single nashorn engine instance without using nashorn classes that are not part of the javax.script API?
The following program runs successfully for me:
import javax.script.*;
import jdk.nashorn.api.scripting.*;
public class NashTest {
public static void main(String[] args) throws ScriptException {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine =
factory.getScriptEngine(new String[] { "--global-per-engine" });
engine.eval("java.lang.System.out.println('hello world!')");
}
}
Make sure you're using a released version of JRE/JDK 8.
still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.
By running System.loadLibrary("myAPI"), I verified that the DLL file "myAPI.dll" can be successfully loaded into my Eclipse Java project. Now I need to call methods specified inside this DLL file from my Java code. To do this, I added JNA to my Java project. Then I wrote the below-given code snippet that should be able to get instances of classes IProject and ProjectFactory (specified in the DLL file).
I still don't understand how to properly implement this with JNA. I checked different threads, e.g. this one, but the ones I checked don't provide an answer. Any help is highly appreciated. Thanks.
import com.sun.jna.Library;
import com.sun.jna.Native;
public class MyClass {
public interface myAPI extends Library {
//...
}
void LoadProj() {
myAPI api = (myAPI) Native.loadLibrary("myAPI",myAPI.class);
String fileName = "xxx.sp";
IProject project; // this is wrong but shows what I am trying to do
try {
project = ProjectFactory.LoadProject(fileName);
}
catch (Exception ex) {
MessageBox.Show(this, ex.Message, "Load failure");
}
}
}
Not sure what problem you are facing but as a practice your myAPI interface should declare all the methods verbatim with appropriate parameter mapping. I don't see any methods inside your interface.
Please checkout the this link as well as the link mentioned above by #Perception
If there are no Java classes or Java source hidden inside this DLL (which would be ... strange), then it will never work this way. You can't instantiate C# classes or use C# interfaces. MessageBox.Show( isn't Java either, it is Windows Forms code.