How to use WSDL2Java generated files? - java

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

Related

Import .jar libraries in VS Code

I need to use a .jar library, given by my teacher, to code for my Java class.
I am using VS Code, with the Java Extension Pack installed, for Java Project Management.
Can someone please explain me step by step how is it possible to import the .jar library, in order to use the classes defined by my teacher.
I have tried to copy the .jar in the lib folder and then add the reference, but it still did not work. I also know that I have to declare the classpath, but when I create the Java Project the .classpath file is not created automatically.
Thanks already!
First you should examine the classes in .jar file. Then you should load that class as,
Class<?> c1 = Class.forName("java.lang.String");
Then after you can use that class by calling that Class reference type variable.
See this example as well,
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
// get the Class instance using forName method
Class c1 = Class.forName("java.lang.String");
System.out.print("Class represented by c1: "+ c1.toString());
} }
Try to understand the code and implement proper solution to your project.
Good Luck.

Java | Custom Created Package Does not Exist

Purpose
I want to be able to create a package and call it.
Alternatively, I would like to create separate files for my method (to avoid having x classes in one file).
Setup
Here is my LetterGrader.java file:
package grade.util;
import java.util.*;
import java.io.*;
public class LetterGrader {
private void readArgs() {
System.out.println("Hello, read CLA!");
}
}
Here is my TestLetterGrader.java file:
import java.util.*;
import java.io.*;
public class TestLetterGrader {
public static void main(String[] args) {
LetterGrader letterGrader = new LetterGrader(); // instantiate
letterGrader.readArgs(); // call method
}
}
Steps Taken
First, I compile LetterGrader:
This auto creates the bin/grade/util/LetterGrader.class file
javac -d bin -sourcepath src src/grade/util/LetterGrader.java
Here is my working directory at this point
Second, I compile TestLetterGrader:
This fails
javac -d bin -sourcepath src src/grade/util/TestLetterGrader.java
The error message:
src/grade/util/TestLetterGrader.java:6: error: cannot find symbol
LetterGrader letterGrader = new LetterGrader(); // instantiate
^
symbol: class LetterGrader
location: class TestLetterGrader
Question
I believe I am misunderstanding how to call a classes from separate files (in the same location). How can I accomplish this?
You are importing class that is in bin folder. Don’t do that it would not work. You don’t need any import, because the classes are in the same place. Make package under src folder and place the classes there. Remove package grade.util and rename it to the package where you place the classes.
File structure:
src
\
\
yourpackage
\
\
LetterGrader.java TestLetterGrader.java
Then delete everything in your build folder and compile the classes. Java will make it’s magic. You do need to worry about bin folder, it is only for storing compiled classes.
Classes will look like this:
//package name that you created
package yourpackage;
public class LetterGrader {
//need to be public when calling from another class
public void readArgs() {
System.out.println("Hello, read CLA!");
}
}
And
//folder that you placed the .java files
package yourpackage;
//without any import
public class TestLetterGrader {
public static void main(String[] args) {
LetterGrader letterGrader = new LetterGrader(); // instantiate
letterGrader.readArgs(); // call method
}
}
Your second question:
You can use classes from other folders, but you you have to import them and they have to be under src folder.
Tell you have class A.java in folder Second and class B.java in folder Main. You will import the the folder in this case import Second.A;
And then call the class A a = new A();
When you have method in a that you want to call simply do:
a.yourmethod();
You have to change private void ... to public void... because you cannot call private outside of the class.
When you are running compiled classes they have to be in the same folder.
Thanks #maratonec for the guidance.
My initial mistake was that I was misunderstanding/misassigning the classpath assignment variable when running a program via terminal. The below helped me.
Compiling and Running a Java Program (on a PC)
• Set the working directory (say, JavaBook)
C:\> cd JavaBook
• Compile HelloWorld.java
C:\JavaBook> javac -d bin src\HelloWorld.java
•Run the program
C:\JavaBook> java -classpath bin HelloWorld
Also, the approach of having all my class files in the same location simplified things. I didn't have to worry about classpath. But not ideal as I have many files to work with.
As for the package creation, I am going to play around with java a bit more before using it. I think I need to solidify my understanding.
Thanks for helping me!

Java compiler cannot see my class

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 ?

load class only

I have developed eclipse plugin which for any given java project create GUI in form of package structure. I have successfully run my plugin for different java project.
Now, I thought should try my code in some open source project, therefore, I download JDOM Framework.
However, I found that the JDOM source code has this structure.
JDOM -> contrib -> src -> java -> org -> jdom2......
where as i assume that the project will have always below structure
Project Name -> Src -> PACKAGE NAME STARTS HERE.....
I load the classes using below code,
IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
// unit.getPath().toString() give me path, but how to extract only class name with package
// save it in to MAP with Package as key
}
}
}
Now, I want to show classes with only package name, therefore, I remove first two string (PROJECT NAME, SRC), but this cannot be always the case as for JDOM Framework.
Therefore, how can I get only package name along with class name using my method above? Or should I use different mechanism?
Looking at the directory structure alone seems to be an awfully error-prone way to go about it. Who knows how deep the directory tree goes? If instead you scan for Java source files, you should be able to construct a reader that finds the package declaration at the beginning of the file. If there isn't one, you don't need to worry about it. Do I need to say you can store package names in a HashSet to avoid duplicate package declarations?
The ICompilationUnit has a findPrimaryType method:
IType primaryType = unit.findPrimaryType();
and IType has getFullyQualifiedName():
String name = primaryType.getFullyQualifiedName();

Not able to load my custom java classes in JRuby

I have created one java project. Folder structure is like
Test
- src
-TestJavaCall.java
I created jar of this prject test.jar.
Code in my ruby file is as follows.
include Java
require '/home/office/test.jar'
java_import Java::TestJavaCall
testJava = TestJavaCall.new
When i am executing this script, i am getting this error.
NameError: cannot link Java class TestJavaCall
get_top_level_proxy_or_package at org/jruby/javasupport/JavaUtilities.java:49
const_missing at file:/home/spaul/.rvm/rubies/jruby-1.7.4/lib/jruby.jar!/jruby/java/java_module.rb:4
Please let me know what is the way to use custom java classes in ruby?
Source code of TestJavaCall.java
public class TestJavaCall {
public void testJavaCall()
{
System.out.println("test java call");
}
}

Categories

Resources