Java compiler cannot see my class - java

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 ?

Related

Using jar from scala project from python via Py4j

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?
}

how to work wsimport for this WSDL i get errors and i cant make it work

i am trying to use the command wsimport to get the java classes for a SOAP Service, but i get lots of errors.
The Url WSDL is this
https://onlineavl2api-mx.navmanwireless.com/onlineavl/api/V1.3/service.asmx?WSDL
and this is one of the errors i get(there is a bunch)
[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 820 of https://onlineavl2api-mx.navmanwireless.com/onlineavl/api/V1.3/ser
vice.asmx?WSDL
To generate WebService Clients i use:
wsimport -keep -p clientC https://onlineavl2api-mx.navmanwireless.com/onlineavl/api/V1.3/service.asmx?WSDL
wsimport is trying to generate classes in the same package with same name, to fix this I would try:
1.- ommit -p clientC
or
2.- Add an annotation on the webservice like this:
package com.me.basiccalcws;
import javax.jws.WebService;
#WebService(targetNamespace = "http://client.basiccalcws.me.com/")
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
took from Name Collision In Wsimport Generated Class And My Original Web Service Class
Best Regards

Creating an API

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.

java command line errors

I'm new to java and I'm trying to compile and run a web service example from a book.
The example uses 3 files.
I can create an Eclipse Project and Run it. It works fine this way.
From the command line I tried
javac TimeServer.java TimeServerImpl.java TimeServerPublisher.java
And got no errors
This program does not run on the command line returns error:
"Could not find the main class"
java TimeServerPublisher
running using the -classpath option returns the same result.
Set classpath does not help either. ie
java -classpath . TimeServerPublisher
fails as well
Most of the online docs specify I need a classpath. I tried everything they suggested.
Please Help. Thanks in advance
Source:
TimeServer.java
package ch01.ts;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer
{
#WebMethod String getTimeAsString();
#WebMethod long getTimeAsElapsed();
}
TimeServerImpl.java
package ch01.ts;
import java.util.Date;
import javax.jws.WebService;
#WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer
{
#Override
public String getTimeAsString()
{
return new Date().toString();
}
#Override
public long getTimeAsElapsed()
{
return new Date().getTime();
}
TimeServerPublisher.java
package ch01.ts;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher
{
public static void main(String[ ] args)
{
Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
}
}
Your class is not named TimeServerPublisher; it's named ch01.ts.TimeServerPublisher. Even if you manage to get the JVM to find your class file, it will reject it with a wrong-name error, as you must invoke the class with its full name.
Put all the class files into a directory ch01/ts if they're not there already, and from ch01's parent directory, type
java -cp . ch01.ts.TimeServerPublisher
I guarantee that done correctly this will work.
get rid of the package statements until you know how they work. to have that package, your sources and binaries should be under ./ch01/ts/ and you would compile and invoke as:
javac ch01/ts/*.java
java ch01.ts.TimeServerPublisher
Move all your class files to folder ch01/ts.
and then execute command
java ch01.ts.TimeServerPublisher
There you go. If you say javac -d ch01/ts *.java during compilation, it will be solved.

How to use WSDL2Java generated files?

I generated the .java files using wsdl2java found in axis2-1.5. Now it generated the files in this folder structure: src/net/mycompany/www/services/
The files in the services folder are: SessionIntegrationStub and SessionIntegrationCallbackHandler.
I would like to consume the webservice now. I added the net folder to the CLASSPATH environment variable. My java file now imports the webservice using:
import net.mycompany.www.services;
public class test
{
public static void main(String[] args)
{
SessionIntegrationStub stub = new SessionIntegrationStub();
System.out.println(stub.getSessionIntegration("test"));
}
}
Now when I try to compile this using:
javac test.java
I get: package net.mycompany.www does not exist.
Any idea?
As already suggested you need to import the generated stub class, not it's package
import net.mycompany.www.services.SessionIntegrationStub;
You then need to populate your XML request objects. I don't know what your WSDL looks like but for example
SessionIntegrationStub.SessionRequest req = new SessionIntegrationStub.SessionRequest()
req.setParamOne(1)
req.setParamTwo(2)
And finally invoke the web service
SessionIntegrationStub.SessionResponse resp = stub.operationOne(req)
println resp.getAnswer()
Note: The setters and getters above correspond to elements declared in your schema. The SessionRequest and SessionResponse classes would correspond to complex types declared in your schema.
This should presumably say import net.mycompany.www.services.*;. You missed the asterisk.
Issue here is your package structure. Your test.java is in different package then your generated source.
You need to keep current file in same package structure or provide full path of your generated source in javac like
javac src/net/mycompany/www/services/.java src/net/mycompany/services/.java

Categories

Resources