System calls (involving a jar file)

Based on the input of the user, I need to do something that involves using a JAR file. The file is JAXP. Even if the [url=“http://jaxp.java.net/docs/spec/html/#technology-StAX”]documentation is good, I don’t understand how I need to use it.

What I’m interested in is the part “3. XSLT Plugability”. I want the user to be able, when clicking on a link, to create a new (transformed) file and save it to the server, together with the original one.

I understand that I may have to use PHP’s system() function, but I’m not very good in Java. Could somebody explain me the steps I have to do? ^^

Work on the command line you need to create first, get this working, then move on to recreating/modifying and executing this command via PHP.

Small steps. :slight_smile:

The problem is the first of the two steps :smiley:

I’ve created a file containing this code


public static void main (String[] args) {
Transformer transformer;
TransformerFactory factory = TransformerFactory.newInstance();
String stylesheet = "file:///home/user/mystylesheet.xsl";
String sourceId = "file:///home/user/sourcefile.xml";
try {
    transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(sourceId), new StreamResult(System.out));
} catch (Exception e) {
    System.out.println("Error");
}
}

But when I try to compile it I get the following errors:


xslt.java:1: class, interface, or enum expected
public static void Main (String[] args) {
              ^
xslt.java:3: class, interface, or enum expected
String stylesheet = "file:///home/user/mystylesheet.xsl";
^
xslt.java:4: class, interface, or enum expected
String sourceId = "file:///home/user/sourcefile.xml";
^
xslt.java:5: class, interface, or enum expected
try {
^
xslt.java:7: class, interface, or enum expected
    transformer.transform(new StreamSource(sourceId), new StreamResult(System.out));
    ^
xslt.java:8: class, interface, or enum expected
} catch (Exception e) {
^
6 errors

Are you aware PHP has native, enabled by default, XSL functionality? Maybe it would be less complicated to use PHP in its entirety to perform your task. Maybe. :slight_smile:

Yes, I know, it would be so much easier. Unfortunately for this specific task I need to use Java :frowning:

All those errors are Java related though, let’s move this over to the Java forum for a while and see if we can help there.

Thank you :slight_smile:

No problem.

Can you post a standalone XML and XSL that are known to work? It should help SP members create a solution for you, the code you use for the Java entry-point would be good too.

Ta!

Hi D3V4,

Why do you need to use Java instead of the existing PHP libraries? I recommend sticking with PHP if possible…

As for your Java compile problem,

I’ve created a file containing this code

  1. You need to name the file the same as your class. By convention you should use camel case, so name your file Xslt.java
  2. methods have to belong to class. the class name is “Xslt”
  3. you have to import the correct classes. see top of the file.

see below


import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Xslt {
    public static void main(String[] args) {
        Transformer transformer;
        TransformerFactory factory = TransformerFactory.newInstance();
        String stylesheet = "file:///home/user/mystylesheet.xsl";
        String sourceId = "file:///home/user/sourcefile.xml";
        try {
            transformer = factory.newTransformer(new StreamSource(stylesheet));
            transformer.transform(new StreamSource(sourceId), new StreamResult(System.out));
        } catch (Exception e) {
            System.out.println("Error");
        }
    }
}

Hi! :slight_smile:

I needed to use Java because I was told to do so :smiley: