Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

runtime "NoClassDefFoundError" by runnng jar file

HI

I am working on a project where i am dynamicly creating and compiling the java source files using "com.sun.tools.javac.Main". The program runs perfectly when i am using Eclipse 3.02 (have added the reference to the tools.jar file in the project ) but i have created a jar file to run the program by single click and it runs fine on startup but when i try to dynamincally compile the source file it gives me following error:

**********************************************************
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/tools/javac/Main
**********************************************************

I have checked the path and it displays the ref to java dir correctly as well as reference to tool.jar file. All the java commands works fineon the command prompt.

I have used following lines to setup the compiler
****************************************
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
 String[] args = new String[] {
          "-d", targetClassFile,
           targetJavaFileandPath
};        
int status = Main.compile(args);
*******************************************
is there any way to get it to work ?

thanks

SC
0
scorpio05678
Asked:
scorpio05678
  • 10
  • 10
  • 3
  • +2
2 Solutions
 
TimYatesCommented:
>> I have checked the path and it displays the ref to java dir correctly as well as reference to tool.jar file. All the java commands works fineon the command prompt.

Can you post the classpath?

What command do you use to run the JAR?

Do you have a reference to tools.jar in your jar manifest?

The problem is that tools.jar is not on the classpath...
0
 
CodingExpertsCommented:
You can use ANT to do this task.
Just put ant.jar in classpath.

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;

public class JavaCompiler {
      public static void compile(String srcDir, String libDir, String destDir) {
            Javac javac = new Javac();
            Project p = new Project();
            javac.setProject(p);
            Path srcPath = new Path(javac.getProject());
            srcPath.createPathElement().setPath(srcDir);
            
            Path classPath = new Path(javac.getProject());
            
            FileSet fs = new FileSet();
            fs.setDir(new File(libDir));
            fs.createInclude().setName("**/*.jar");
            
            classPath.addFileset(fs);
            
            javac.setSrcdir(srcPath);
            javac.setDestdir(new File(destDir));
            javac.setClasspath(classPath);
            
            try {
                  javac.execute();
            } catch (BuildException e) {
                  System.out.println("Build Exception Raised");
                  e.printStackTrace();
            } catch (Exception e) {
                  System.out.println("Different Exception Raised");
                  e.printStackTrace();
            }
            }
}

-CE
0
 
TimYatesCommented:
>> Just put ant.jar in classpath.

Just putting tools.jar in the classpath will make the original code run ;-)

The problem here is the classpath ;-)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
scorpio05678Author Commented:
i have java 1.3 J2EE(and its own private JRE) and JAVA 1.5.02 SDK installed using 1.5.02 to run the application

my classspath :

CLASSPATH = C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\j2ee131\lib\j2ee.jar;c:\java\bin;c:\java\lib\tool.jar


I have another variable called java home

JAVA_HOME=C:\Program Files\Java\jre1.5.0_02

java -jar ProjectCXM_Layer3_alpha.jar

My mainfest file:
***************
Manifest-Version: 1.0
Sealed: true
Main-Class: MainClas
*****************

thanks
0
 
TimYatesCommented:
>> CLASSPATH = C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\j2ee131\lib\j2ee.jar;c:\java\bin;c:\java\lib\tool.jar

Isn't it "tools.jar" not "tool.jar" ?
0
 
scorpio05678Author Commented:
oops yes it is

but it still doesn't work

0
 
TimYatesCommented:
Does

c:\java\lib\tools.jar

exist?

Open it up with WinZip, and check it contains the classes...
0
 
scorpio05678Author Commented:
yes every thing is there

i can run the program in Eclipse 3.02 by adding the c:\java\lib\tools.jar in the external JAR libs but i have to give it to someone else therefore need it to work from command prompt using jar file. I think for some reason Eclipse 3.02 is not adding ref to tools.jar to mainfest file.

SC

0
 
TimYatesCommented:
Try putting it at the start of the classpath...  the spaces might be messing it up...

CLASSPATH = c:\java\lib\tools.jar;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\j2ee131\lib\j2ee.jar;c:\java\bin
0
 
scorpio05678Author Commented:
nope it's doesn't work

it is driving me crazy now.

this doesn't work either :

java -classpath c:\java\lib\tools.jar -jar ProjectCXM_Layer3_alpha.jar

SC
0
 
TimYatesCommented:
No, that won't...you can't use classpath and jar as parameters together...

Try:

java -classpath /java/lib/tools.jar;ProjectCXM_Layer3_alpha.jar MainClas
0
 
scorpio05678Author Commented:
nope it dosen't work either

SC
0
 
aozarovCommented:
Three options:

1. you can put the tools.jar in the c:\java\jre\lib\ext to make it work (you will not need to include it in the classpath after that though no harm if you do so).

2. As TimYates said the -jar will ignore -classpath (you will nedd to add reference to libraries via META-INF/MANIFEST.MF Class-Path attribute to achive similar effect. see: http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html)

3. java -classpath %CLASSPATH%;c:\java\lib\tools.jar;ProjectCXM_Layer3_alpha.jar JavaCompiler
    should work for you assuming: ProjectCXM_Layer3_alpha.jar is found in the current directory (or provide full  path instead)
                                                 JavaCompiler is your main class (if not replace it with your main class name)
0
 
scorpio05678Author Commented:
Ok

I have tried op1 and op3 and both doesn't work.

*******************************************
op1 -> COPIED as well as MOVED the tools.jar to my c:\java\jre\lib\ext then used java -jar ProjectCXM_Layer3._alpha.jar
it comes with same error.
****************************************************
op-> 3 used the command to run.
Initially it complained that

"Exception in thread "main" java.lang.NoClassDefFoundError: Files\Apache"

so i took following part of the classpath out

"c:\java\bin;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar"

and ran it again but it came up with the help screen that u get when u only type java.

*******************************
Op ->2 i don't know how to secify the path in url ?(confused with slashes) can any one help  Please.

another thing for OP 1 does it matters if i am using the old ref (C:\java\lib\tools.jar) in the Eclipse while building the Jar file.


thanks
SC

0
 
aozarovCommented:
1. Use the jvm under c:\java\jre\bin (hence c:\java\jre\bin\java) or else copy it also to c:\java\lib\ext\ [you will need to crate that foolder])
3.  what is the command line that you are using?
2. C:\java\lib\tools.jar = in URL to file:/C:/java/lib/tools.jar

>> another thing for OP 1 does it matters if i am using the old ref (C:\java\lib\tools.jar) in the Eclipse while building the Jar file
No.
0
 
aozarovCommented:
3. Is your main called Files.Apache?
Did you try to run it like this:
java -classpath %CLASSPATH%;c:\java\lib\tools.jar;ProjectCXM_Layer3_alpha.jar Files.Apache ?
If Files.Apache is not your main program then you need to replace it with the class file that has the main method in it.
You can tell which class is the main class by looking in your jar manifest file for an entry called "Main-Class".
0
 
CodingExpertsCommented:
scorpio05678 did you try my solution.

-CE
0
 
scorpio05678Author Commented:
aozarov

no my main class in not called Files.Apache.  it is a reference to the "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar" for running J2EE app.

The main class is called MainClass.

for OP-1 i wrote (on the DOS prompt in the directory where ProjectCXM_Layer3_alpha.jar exist ) "java -jar ProjectCXM_Layer3_alpha.jar"

for OP 3 i wrote "java -classpath %CLASSPATH%;c:\java\lib\tools.jar;ProjectCXM_Layer3_alpha.jar"

for OP 2 i have tried to add the url to the jar file but it keeps on complaining that "Invalid jar file header". My edited and original jar files are :

Original (created by Eclispe, there are 2 line empty at the end.)
*****************************************************
Manifest-Version: 1.0
Sealed: true
Main-Class: MainClass


*************************************

After my addition it looks like this.
***************************************
Manifest-Version: 1.0
Sealed: true
Class-Path:/C:/java/lib/tools.jar
Main-Class: MainClass


***************************************

CodingExperts
 I have added the path to ant to the project in the eclipse as well as created a class (JavaCompiler.java) in my project, but not sure how to use that class. here is how i am compling at the moment.
**************************************************
public int compileFile(String packageName,String fileName,String targetJavaFileandPath, String targetClassFile)
{
      int resultCode = -1;
      try
      {      
           this.className =  packageName +"."+ fileName.substring(0, fileName.length()-5);
           this.targetClassFile = targetClassFile;
            // Compile
            com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
            String[] args = new String[] {"-d", targetClassFile,targetJavaFileandPath};        
             int status = Main.compile(args);
             switch (status)
             {
      case 0:
      {                               
        resultCode = 0;
        break;
                }
       case 1: resultCode = 210; break;//System.err.println("Compile status: ERROR"); break;
       case 2: resultCode = 220; break;//System.err.println("Compile status: CMDERR"); break;
       case 3: resultCode = 230; break;//System.err.println("Compile status: SYSERR"); break;
       case 4: resultCode = 240; break;//System.err.println("Compile status: ABNORMAL"); break;
       default:
              resultCode = 200; break;//System.err.println("Compile status: Unknown exit status");
                }
       }
       catch(Exception exp)
      {
          System.out.println("GlobalInterface -> CompileFile: Exception !!!"+exp.toString());
          resultCode = 300;
       }
       return resultCode;
}      
*********************************************

SC
0
 
TimYatesCommented:
Copy tools.jar into the same folder as your jar file, and change the manifest to read:

Manifest-Version: 1.0
Sealed: true
Class-Path: tools.jar
Main-Class: MainClass
0
 
TimYatesCommented:
then you should be able to do

    java -jar ProjectCXM_Layer3_alpha.jar
0
 
scorpio05678Author Commented:
Ok

GOOD NEWs

It works :)


but am i doing something wrong when i say
"Class-Path:/C:/java/lib/tools.jar"

SC
0
 
TimYatesCommented:
Try:

Class-Path: /java/lib/tools.jar
0
 
TimYatesCommented:
Class-Path: C:/java/lib/tools.jar

might work too...
0
 
CodingExpertsCommented:
String srcDir ="C://myproj//src"; // where all the source files are located
String libDir = "C://myproj//lib";// where all the libraries required to compile your source files are present
String destDir = "C://myproj//classes";// where the class files are to be generated
            
JavaCompiler.compile( srcDir, libDir, destDir );

Pretty simple isn't it......
-CE      
0
 
scorpio05678Author Commented:
HI guys

Thank you all for the help on sorting this thing out.

will be back (i am sure) already working on something that will get me into trouble(will be stuck)


SC
0
 
scorpio05678Author Commented:
Yes

i did that but was thinking as it involves ANT so there must be something else needed to to be done. (haven't got a clue about ANT)

it keeps on compling that it cann't find the file ***.class in the project directory.


but thnaks anyways

SC


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 10
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now