Generate java classes from WSDL through java program - java

I used wsimport command line tool for creating classses but I want to do it from the java code. Any idea?

It is possible to call the WsImport main() method:
import com.sun.tools.ws.WsImport;
...
String[] args = {"put", "your", "arguments", "here"};
WsImport.main(args);
I think that this is what the Ant task does.
Update: I'm not sure to understand what you're trying to do (and I don't think that you want to generate source code and compile it during runtime).
If the question is actually about doing dynamic invocation, JAX-WS's dynamic invocation interface (DII) is the javax.xml.ws.Dispatch object. Check JAX-WS's dynamic Dispatch interface.

What you are trying to achieve is not typical Java Web Services flow. But you can achieve this with dynamic JVM based Groovy language using GroovyWS module.

You can use the wsimport ant task programatically.
You can do this by using the task class - com.sun.tools.ws.ant.WsImport. Instantiate it, set its properties (as defined on the task documentation), and call the execute() method.

Related

What is the alternative of com.sun.tools.javadoc.Main.execute to run Doclet in jdk 11?

I am using JDK 11 on Apache netbeans 10.
The main method is depricated since java 9 and marked for a removal where there is no alternative
All of my attempts in command line ended up with
javadoc: error - Cannot find doclet class Exception
When I try:
com.sun.tools.javadoc.Main.execute (new String[]{"-doclet",TestVarElement.class.getName(),"C:\\Users\\Super3\\Documents\\NetBeansProjects\\MyProject\\src\\pk\\TestVarElement.java"});
I get:
javadoc: error - Doclet class pk.TestVarElement does not contain a start method
Start method is depricated and replaced by run method, the previous set up is working for java 8 and older, I want the equivalent for 9,10,11.
I looked at the documentation for DocumentationTool and the relevent materials but I did not find a single working example.
Is there any way we can run a Doclet/DocletEnvironment programmatically or a working example from commandline?
Assuming what you're trying to do is run Javadoc programatically, the jdk.javadoc module documentation describes ways to do so:
javadoc
This module provides the equivalent of command-line access to javadoc via the ToolProvider and Tool service provider interfaces (SPIs), and more flexible access via the DocumentationTool SPI.
Instances of the tools can be obtained by calling ToolProvider.findFirst or the service loader with the name "javadoc".
Based on your previous code, and your edit, you don't need the functionality of the DocumentationTool SPI. The ToolProvider SPI should suffice. As mentioned in the documentation (and by #AlanBateman in the comments) you can get the ToolProvider instance using the following:
ToolProvider javadoc = ToolProvider.findFirst("javadoc").orElseThrow();
You then call one of the run methods with the out/err streams of your choice and the desired arguments. The arguments are the same you'd use for the unsupported Main.execute API you're currently using.
int result = javadoc.run(System.out, System.err, /* ... your args ... */);

Is it possible to run multiple classes using Ant after compilation?

I'm totally new to StackOverflow so please try to tolerate me.Thank you.I'm new to ANT with a beginners knowledge in java. So I wanted to know if it is possible to run multiple classes of java source files in an xml file using Ant.
Instead of specifying multiple java classnames within the targets, can I run the run classes in a single go?
If you are indeed asking whether a java task can be configured to run more than one class I believe the answer is no. According to the documentation for the java task the classname attribute specifies
the Java class to execute.
If you need to run multiple classes using a single java task you could create a controller class to run these classes then simply invoke that single controller.

execute a Kermeta file from a Java program

I developed a transformation that generates a Kermeta XMI file.
My problem is that I would run this transformation (.kmt file) in the background, that is to say from a Java program.
I tried a lot of code but always without result.
Someone can help me?
Thank you in advance for your help.
Running a kermeta program depends on the version of Kermeta and about its execution context.
With Kermeta 2, the kmt code is compiled into scala. This scala code can be directly called from java thanks to scala compatibility with java.
You first need to declare at least one main operation in order to generate the appropriate initialization operation.
Let's say you have a method "transformFooToBar" in a class "org::example::FooToBar" in your kermeta code.
Declare it as a main entry point using the tags
#mainClass "org::example::FooToBar"
#mainOperation "transformFooToBar"
This main operation must have only string parameters.
This will generate an utility scala class org.example.MainRunner which contains useful initialization methods and a main operation that you can call from the command line.
Case 1/ With Kermeta 2 code called from an eclipse plugin:
This is the simpliest case, call the method org.example.MainRunner.init4eclipse()
before any other call. Then call your main method org.example.KerRichFactory.createFooToBar.transformFooToBar()
You can call any other classes or methods (ie. even with parameters that aren't String)
This is quite convinient for building tool for eclipse based on kermeta transformation.
Case 2/ Kermeta 2 code called from a standard java application (ie. not running in an eclipse plugin)
The initialiation method is then org.example.MainRunner.init()
Common trap for case 2: many transformations that run as standalone still need to manipulate models using eclipse uri scheme in their internal refencing system. Ie. platform:/plugin/... , platform:/resource/..., pathmap:/... , or even more complex uri mapping (typically using custom protocols ) (you can check that easily in by looking in the xmi files as text)
In that case, since Eclipse platform isn't running to provide the search mechanism, you need to provide manually the equivalent URI mapping to map these URI to your local system URI (Ie. to file:/... or jar:file:/ URIs)
One possibility is to use an urimap.properties file that provide such mapping. By default when running a kermeta program in eclipse, an urimap.properties is generated for the current eclipse configuration.
When deploying a kermeta program to another computer, or using a custom deployment packaging, you will have to provide/compute an equivalent file for the target computer.
For convinience, you can set the location of this urimap.properties files thanks to the system propery "urimap.file.location"

How to add a method to an existing web service develoment with axis2 and eclipse

i've a web service written with axis2, starting from wsdl using wsdl2java utility. I developed it using eclipse. It works fine.
What's the best way to add a new method to the web service? I must add the method in the wsdl and recreate all the java file with wsdl2java and copy the actual code in the new class? Or there's a quicker way?
Thank you in advace.
Nicola
If you are using the "contract first" approach, the best way to add a method is indeed to modify the WSDL and then regenerate your classes with wsdl2java.
The tools will recreate all your skeleton classes so make sure that this does not override any code you added to it or you will loose your modifications. To avoid this you should generate an interface for the skeleton with the -ssi parameter. Then, have your own class implement this interface and use it instead of the implementation that the wsdl2java creates.
You can even add new method in code and make entry of method in WSDL and conf\axis

How can I dynamically create a new .java file from a Java program?

I want to create a one .java from the Java program. When I run the program, automatically one Java file will created in my project, and also create some run time (dynamic) variable in that file. How can I do this?
I know for this I have to use a Reflection API like Class and Method, but what are the methods in Class and Method to do this?
You cannot create new classes or methods using the reflection APIs. They are not designed for this. (The Class and Method APIs are for performing operations on object instances in a dynamic fashion.)
If you want to create new code on the fly, there are two basic approaches to doing this:
Generate Java source code, write it to a file, use the Java compiler to compile it to a bytecode file, and then load the bytecodes. (There are standard APIs for running the Java compiler within the JVM of a running application.)
Use BCEL or equivalent to construct a bytecode file from scratch, and then load the bytecodes.
Both approaches are tricky and computationally expensive. The BCEL approach is particularly tricky because you need to understand a lot about the JVM to do the job.
Apparently you want to create a new class at Runtime and use it. You can sure create a .javafile, compile it and load it from a custom class loader but that's probably not the best/easiest thing to do. Here are a bunch of solutions:
First of all if you want to extend an interface, you can use the Proxy from the Java Reflection API.
It you want to extend a class rather than implements an interface or create a class out of the blue you need to use a library to create bytecode. You can find a bunch of them on http://www.java-opensource.com/open-source/bytecode-libraries.html. Among these libraries I like javassist mainly because it is the only library to my knowledge letting you enter Java code directly rather than bytecode.
A last solution should be to use a framework like Groovy or BSH to interpret pseudo-java code.
No, you can't generate new .java files using Reflection. You could perhaps create a new class, and use this class, in runtime, but you can't write that class out to file in the form of a .java source file.
Have a look at the JustAdd framework for instance. This framework solves this type of problems IIRC.
Java is a strongly typed language( As opposed to a weakly typed language). Simply put you need to have a Class (prototype) to create a instance of object. What you are trying to do is not natural in java (or any strongly typed language).
If you have to have this functionality in java, you need to use groovy. Groovy is a dynamic language that can run in Java JVM. You need to check Expandos in groovy.(ofcourse it still will not create a .java file).

Categories

Resources