Solved

com.sun.xml.* do not exist !!!!

Posted on 2004-04-29
48
1,209 Views
Last Modified: 2010-08-05
hi guyz,

i'm currently working on a servlet that is supposed to generate an XML Document from a database (well it's a bit more complicated than that but no need to go deeply inside the code !)

so i was trying to use DOM to create my XML Object and then print it out just to try, and this is what i got:

package com.sun.xml.tree does not exist

here is a sample of the code i use. actually i found this code on the SUN documentation, so it should work i guess.
i really don't get it.



import java.io.*;
import com.sun.xml.tree.*;
import org.w3c.dom.*;

public class DomTest {

    public static void main(String args[]) {
        try {
            Document document = new XmlDocument();

            //-Génération de l'arbre de noeudst--------------------------------
            Element root   = document.createElement("PERSONNE");
            Element first  = document.createElement("FIRSTNAME");
            Element second = document.createElement("LASTNAME");
            Text firstTxt  = document.createTextNode("Dominique");
            Text seconTxt  = document.createTextNode("Liard");

            document.appendChild(root);
            root.appendChild(first);
            root.appendChild(second);

            first.appendChild(firstTxt);
            second.appendChild(seconTxt);

            root.setAttribute("Machin", "truc");

            //-Affichage du document XML source--------------------------------
            ((XmlDocument)document).write(System.out);

        } catch(Exception e) {
            e.printStackTrace();
        }      
    }
}

thankx in advance.
miraodb
0
Comment
Question by:miraodb
  • 18
  • 18
  • 12
48 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I guess that's a non-standard library
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
and with "non-standard" I mean: not included in JSDK 1.4.2
So, you have to download and "install" it before you can use it.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
0
 

Author Comment

by:miraodb
Comment Utility
hi zynnx,

see i told u i will need ur help soon.
so what can i do, do u have any idea of how i can create an xml file. i saw something called JAXP but i couldn't find the way to make it work.

miraodb
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>>see i told u i will need ur help soon.
:)

Have a look at the link "you should not live without as java developer": javaalmanac.com

http://javaalmanac.com/cgi-bin/search/find.pl?words=xml

Specifically:
http://javaalmanac.com/egs/java.beans/WriteXml.html
and
http://javaalmanac.com/egs/java.beans/ReadXml.html
0
 

Author Comment

by:miraodb
Comment Utility
ok i have a  look and i let u know but i think i found something not bad, instead of com.sun.xml.tree.* i put org.apache.crimson.tree.XmlDocument
but not sure if it will work yet.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
OK. Success.
0
 

Author Comment

by:miraodb
Comment Utility
yeah these links are good. i have a look and see if i can get it done. i let u know.
0
 

Author Comment

by:miraodb
Comment Utility
still have problems!

actually i found that with org.apache.crimson.tree.XmlDocument
i could manage to do what i want, however i can create the class but cani't run it.
i have the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: GenXML (wrong name: axiomsl/toolkit/dataviewer/GenXML)

i looked on internet and it looks like it's the path but my path looks good:
.; %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\UltraEdit;C:\installaxiom\unix;C:\j2sdk1.4.2_04\bin;C:\installaxiom\fincad\fcml6\lib;C:\installaxiom\omniORB-4.0.0\bin\x86_win32;C:\installaxiom\xml4c3_5_1-win32\bin

it has a lot of stuffs, but the ;. is here as well as the C:\j2sdk1.4.2_04\bin

i really don't know what's wrong.

miraodb
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Can you find that GenXML class somewhere in one of the jar's?
(You know jar files are just files you can open with Winzip, don't you?)

Nevertheless I don't think you need some 3rd party libs to generate an XML document.
(I thought the javaalmanac links use nothing special besides the JSDK)
0
 

Author Comment

by:miraodb
Comment Utility
What do u mean in a jar? i created the java myself and then i build it so i have the class and then try to run it but i got the error.
let me explain in more details:

it builds fine in both netbeans or promt but can't run in both, got the error.
i tried with the famous HelloWorld form internet, it build fine in both, but i can run it only in command promt, in netbeans i have the error.
so i tried to change the name of the HelloWorld into something else and then oh surprise, doesn't run again.

damn it, i'm really stock.
i need yur help man, coz i have a deadline fo rthis silly thing :(

what can i do???
do u use yahoo messenger? mayube it would be easier if u don't mind of course.

miraodb
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> do u use yahoo messenger?
No sorry.

>>What do u mean in a jar?
Sorry again. Didn't realize GenXML was one of yours

>>it builds fine in both netbeans or promt but can't run in both, got the error.
>>Exception in thread "main" java.lang.NoClassDefFoundError: GenXML (wrong name: axiomsl/toolkit/dataviewer/GenXML)
Can you post your code (the one containing the main() function)?

>> i tried with the famous HelloWorld form internet, it build fine in both,
Post that code please (including java file name; including import statements)
>> but i can run it only in command promt, in netbeans i have the error.
This points to a pure path problem. I guess in netBeans some 3rd party jar (that you are using) is not mounted.

>>so i tried to change the name of the HelloWorld into something else and then oh surprise, doesn't run again.
You tried to change the name of the class, name of the java file, something else?
Post that changed code also (including java file name; including import statements)

>>damn it, i'm really stock.
>>i need yur help man, coz i have a deadline fo rthis silly thing :(
We'll get you through
0
 

Author Comment

by:miraodb
Comment Utility
ok no worries for yahoo.

so here is the code for the GenXML.java:
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import java.text.*;
import java.io.*;
import java.net.*;
import java.awt.datatransfer.*;

import javax.swing.*;
import javax.swing.table.*;

import com.f1j.swing.*;
import com.f1j.ss.*;
import com.f1j.util.*;

import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.*;

/*
import axiomsl.util.ui.*;
import axiomsl.util.ui.print.*;
import axiomsl.util.*;
import axiomsl.dataview.*;
import axiomsl.util.condition.*;
import axiomsl.lang.NullTerminatedString;
import axiomsl.lang.DataType;
import axiomsl.lang.AxiomDate;
import axiomsl.aggregation.*;
import axiomsl.task.*;
*/

public abstract class GenXML
{
    public void main(String args[]){
    try {
        XmlDocument xmlDoc = new XmlDocument();

        Element treedata = ( Element) xmlDoc.createElement("treedata"); //root node
        Element branch = ( Element) xmlDoc.createElement("branch");
        Element ShortDesc = ( Element) xmlDoc.createElement("ShortDesc");
        Element LongDesc = ( Element) xmlDoc.createElement("LongDesc");
        Element metric = ( Element) xmlDoc.createElement("metric");

        xmlDoc.appendChild(treedata);
        treedata.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
        treedata.setAttribute("xsi:noNamespaceSchemaLocation","E:\\User\\Work\\Axiom\\AxiomXML\\viewer.xsd");

        //-Outputthe XML document
        ((XmlDocument)xmlDoc).write(System.out);
       
        /*
        treedata.appendChild(branch);
        branch.setAttribute("branch",getColumnTitle(j));
        branch.appendChild(ShortDesc);//
        branch.appendChild(LongDesc);
        branch.appendChild(metric);
        metric.setAttribute("metriccode",getColumnTitle(j));



        Writer out = new OutputStreamWriter(System.out);

        int i = getColumnCount();
        for(int j = 0; j < i; j++)
        {
            String s1 = getColumnTitle(j);//get the columnTitle such as "app_book_name", "commodity_name" or "declined_qty"
            DataCell datacell = null;
            int l = mapPhysicalColumnToLogical(k);
            if(l >= 0)
            {
                FieldInfo fieldinfo = getFieldInfoForColumn(j);
                boolean flag = fieldinfo != null && fieldinfo.getAlias().equals(m_RootAlias) && fieldinfo.m_Field.is_key;
                if(flag)
                {
                    s2 = s2 + (s2.equals("") ? "" : "&#$") + fieldinfo.getAlias() + "." + fieldinfo.getColName() + ":" + cell2ServerString(adatacell[l], fieldinfo.m_Field);
                    s3 = s3 + (s3.equals("") ? "" : " and ") + ClassUtils.replaceOccurences(fieldinfo.getDisplayTitle(), "\n", " ") + " = " + cell2ServerString(adatacell[l], fieldinfo.m_Field);
                }
                if(flag)
                    s1 = s1 + "<a href=zaq>";
                s1 = s1 + cellToHTML(datacell = adatacell[l], i, j);
                if(flag)
                    s1 = s1 + "</a>";
            }
            else
            {
                s1 = s1 + cellToHTML(datacell = generateArtificialCell(adatacell, i, k), i, k);
            }
            Field field = getFieldForColumn(k);
            if(field == null || field.type.value() != 1 || !isCalculatingTotalInCol(k))
            continue;
            if(m_totalForHTML == null)
            m_totalForHTML = new double[j];
            m_totalForHTML[k] += datacell.is_null ? 0.0D : ((Double)anyToObject(datacell.value, field)).doubleValue();
        }*/
    }
    catch (Exception e) {
        e.printStackTrace();
    }
  }
}

ok wierd thing, if i use the following HelloWorld.java code:
class HelloWorld {
     public static void main(String[] args)
     {
           System.out.println("Hello World!");
     }
}
it buils and runs perfectly once again only in command prompt, don't know what's wrong with Netbeans.
so the wierd thing is if i copy paste the code to this HelloWorld.java file without changing the name file and classnae, it runs !!!! then i change the fiel name and class name to GenXML, it doesn't. But and this is even wierder, if i change it to GXML instead of GenXML, it does WORK !!!! what the hell is that ???

i'm new in Java but i think this doesn't really make sense.

miraodb.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
About the weird HelloWorld thing.
1) In a mypack folder in netBeans I made a new file HelloWorld.java
and copied that HelloWorld code in it.
2) I compiled it
3) I ran it and got:
     java.lang.NoClassDefFoundError: mypack/HelloWorld
     Exception in thread "main"
4) in the file I added as the first line

    package mypack;

5) compiled it
6) ran it.
   Everything OK.

Conclusion: you need to define your java file as being part of your package (==folder in netBeans)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Try the same for your GenXML file.
0
 

Author Comment

by:miraodb
Comment Utility
ok thankx about the netbeans thing however, i still don't understand how come it doesn't work when i use GenXML instead of GXML !
that's wierd no?

0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Also, just make sure your classpath settings are correct.
0
 

Author Comment

by:miraodb
Comment Utility
thankx they are coz now it's working for the GXML class, however still doesn't for the GenXML.
really wierd.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Can you just summarize your current situation?

- are you still working on Netbeans now/ are you compiling/ running from the command prompt now?

- what exception does it throw?

- which folder is your class-file in?

- what is the classpath?

Generally, Netbeans would not use the System classpath, and it would use its own classpath. The system classpath would be used when you run from the command prompt.
0
 

Author Comment

by:miraodb
Comment Utility
i'm still working with netbeans since it works with the GXML.
however it stills doesn't for the GenXML, even though it's the same code except that the name file and class name changes.

the error thrown is always the same:
java.lang.NoSuchMethodError: main
Exception in thread "main"

my classpath is:
.;C:\j2sdk1.4.2_04\bin;C:\installaxiom\F1J8\F1J8Swing.jar;C:\installaxiom\XML4J-3_1_1\xerces.jar;C:\installaxiom\iText\iText.jar;C:\installaxiom\bea\weblogic700\server\lib\weblogic.jar

the class file is in the same folder as the java one since i'm not working in a project with netbeans, so when i compile it just build the class file in the same folder.

so how do u make netbeans use the correct classpath ?

0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Do you have GXML and GenXML in the same folder? Then, if GXML runs fine, the class-path is correct and you need not worry about GenXML (since its in the same folder, which is set in the classpath).

The error is - NoSuchMethodError. Means that - the main () method is not there in the GenXML class.

But from your code, I can see that it is there. But can you tell me why the GenXML class is abstract? Just remove the abstract from there and see what happens.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
mayank, I couldn't have said it any better ;°)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
:-)
0
 

Author Comment

by:miraodb
Comment Utility
i tried to remove the abstract but it doesn't ch
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:miraodb
Comment Utility
i tried to remove the the abstract but it doesn't change anything which is logical since the GXML has it too and it works fine.
i really don't get it. i know java is case sensitiveand i've been very careful about it.
so really, i'm wondering what can it be ????
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Are GenXML and GXML in the same folder? And are they in the same package? I don't see any package specified for the GenXML class in your code. Check the package-name in GXML and give the same package-name for this one.
0
 

Author Comment

by:miraodb
Comment Utility
i didn't put any package name.
why i should? what does exactly package stands for ???
i actually have another problem now which is i want to call the main function of my GXML file from a JSP file so i imported the class and try to call it like this:

GXML.main();
but i get this error:
C:\webaxiom\jakarta-tomcat-3.2.2\work\localhost_8080%2Faxiom\_0002fviewer_0002ejspviewer_jsp_219.java:729: No method matching main() found in class axiomsl.toolkit.dataviewer.GXML.
GXML.main();

so i figured it's maybe coz of the String[] args parameter so i tried this:
String[] toto = new String[2];
GXML.main(toto);

it works but i don't understand, do i really need to keep this String[] args parameter for the main coz i don't really need it. when i try to take it away, i have the following error when trying to run it:
java.lang.NoSuchMethodError: main
Exception in thread "main"

            
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You could also try:

GXML.main ( null ) ;

However, the dummy String[]argument is needed because the main () method takes a String array as argument. Usually, the main () method would be called by the JVM and it would supply command-line arguments in that array. That is why it is needed.

I would suggest that you write another method with no arguments and call that method from your JSP. You should call the main () method (with null as parameter) from this method or write the code for the main () method in this one (copy/ paste) & remove the main () method.

>> when i try to take it away, i have the following error when trying to run it:

You should not take it away if you want to run the class as a Java app. Because the JVM will always look for the main ( String [] ) method for starting.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Were you removing the String[] from the GenXML class's main () method?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> do i really need to keep this String[] args parameter for the main
Yes, you do! That's how the main function should look like by convention.
(in fact that's needed if you want to start your app with parameters)

instead of

String[] toto = new String[2];
GXML.main(toto);

you can simply write

GXML.main(new String[0]);
0
 

Author Comment

by:miraodb
Comment Utility
i tried to remove the the main method, actually i just change the name from main(String[] args) to InitXML()
and then when i build it's ok, but when i run i have this:
java.lang.NoSuchMethodError: main
Exception in thread "main"

why does it say main ?????
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Or indeed, like mayank suggests:

public myFunc() {
     main(null);
}

then you can call myFunc()
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
If you remove the main () method, you will not be able to run it as a Java app, like I already said.

public InitXML ()
{
  main ( null ) ;

}

Either run the Java app using the class-name or call GXML.InitXML () ; from your JSP.
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 30 total points
Comment Utility
Your GenXML class perhaps does not run because you have:

>> public void main(String args[]){

The main () method should also be static. Try making it to:

public static void main ( String args[] )

- and see if the GenXML runs, because the JVM will look for a static main () method with String[] argument for starting the execution.
0
 

Author Comment

by:miraodb
Comment Utility
ok i got it, thankx guyz it works fine now with the myFunc() or InitXML....

i have one more question if u don't mind:
up to now i just output the result of my XML, but i would liek to save it to a file for testing purpose. what is the best solution, my code is as follow:

public abstract class GenXML {
    public static void main(String[] args) {
        try{
            XmlDocument xmlDoc = new XmlDocument();
            Element treedata = ( Element) xmlDoc.createElement("treedata"); //root node
            Element branch = ( Element) xmlDoc.createElement("branch");
            Element ShortDesc = ( Element) xmlDoc.createElement("ShortDesc");
            Element LongDesc = ( Element) xmlDoc.createElement("LongDesc");
            Element metric = ( Element) xmlDoc.createElement("metric");
           
            xmlDoc.appendChild(treedata);
            treedata.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
            treedata.setAttribute("xsi:noNamespaceSchemaLocation","E:\\User\\Work\\Axiom\\AxiomXML\\viewer.xsd");
            treedata.appendChild(branch);
            branch.setAttribute("branch","ererer");
            branch.appendChild(ShortDesc);
            branch.appendChild(LongDesc);
            branch.appendChild(metric);
            metric.setAttribute("metriccode","trtr");
         
            //draw the XML source
            ((XmlDocument)xmlDoc).write(System.out);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void myFunc() {
     main(null);
    }
}
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> i have one more question if u don't mind
You could also post another Q.

>> i just output the result of my XML, but i would liek to save it to a file for testing purpose
I think by replacing

 ((XmlDocument)xmlDoc).write(System.out);

by

 ((XmlDocument)xmlDoc).write( FileOutputStream(new File("yourFile.xml")) );
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 30 total points
Comment Utility
Sorry forgot a new:

((XmlDocument)xmlDoc).write( new FileOutputStream(new File("yourFile.xml")) );
0
 

Author Comment

by:miraodb
Comment Utility
ok it works. thankx a lot to both of u.

catch u soon. i guess.

miraodb.
0
 

Author Comment

by:miraodb
Comment Utility
i splitted the points.
thankx again
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks for accepting.

>>catch u soon. i guess.
;°)
0
 

Author Comment

by:miraodb
Comment Utility
arghhhhhhhhhhhh !
just after splitting i tried to change my class and then we i call it with the jsp i have:

org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.
error: File c:\installaxiom\axiom_app\class\client.jar(axiomsl/toolkit/dataviewer/GXML.class) does not contain type axiomsl.toolkit.dataviewer.GXML as expected, but type GXML. Please remove the file, or make sure it appears in the correct subdirectory of the class path.
C:\webaxiom\jakarta-tomcat-3.2.2\work\localhost_8080%2Faxiom\_0002fviewer_0002ejspviewer_jsp_221.java:729: Class axiomsl.toolkit.dataviewer.GXML not found.
                                        GXML.main(new String[0]);
                                        ^
C:\webaxiom\jakarta-tomcat-3.2.2\work\localhost_8080%2Faxiom\_0002fviewer_0002ejspviewer_jsp_221.java:729: Undefined variable or class name: GXML
                                        GXML.main(new String[0]);
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Perhaps GXML is also a class expected some other JAR? >> axiomsl.toolkit.dataviewer.GXML

Try changing the name of GXML and GenXML to something else (the class-name and file-name).
0
 

Author Comment

by:miraodb
Comment Utility
i actually find out what was wrong and it works fine now.

however i'm having a small problem now, which is when i try to use the getColumnCount() function it says that it can't resolve it:

GXML.java [50:1] cannot resolve symbol
symbol  : method getColumnCount ()
location: class GXML
            int nCols = getColumnCount();

i of course have the following import:
import javax.swing.table.*;
 which is suppose to contains this function.

any idea ??
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> int nCols = getColumnCount();

shouldn't you call getColumnCount() on your table object?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
BTW, I don't think that you were having a JTable anywhere, were you? Where did this column-count come from?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Thanks, zzynxie ;-)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
My pleasure :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now