Setting classpath within java application

Hi
Sorry if this question has already been asked, I've searched for it but couldn't find it.
I've written a class (see below) that when given a directory will add all of the jar files to the classpath.
The classpath is updated to include all of the jar files but if I try to use a class within one of these jar files then an exception is generated stating that the class can't be found.
Can anyone help me with this problem? I thought that I would only need to update the classpath.
Thanks,
Rob
public class Jar {
     public static void addJarsToClasspath(String root) {
          File f = new File(root);
          if (f.exists()) {
               File[] jars = f.listFiles(new FilenameFilter(){
                    public boolean accept(File dir, String file) {
                         return ((String)dir.getName()+file).endsWith(".jar");
               String classpath = System.getProperty("java.class.path");
               for (int i=0; i<jars.length; i++) {
                         classpath = classpath.concat(":"+root+"/"+jars.getName());
               System.setProperty("java.class.path",classpath);
               System.out.println(System.getProperty("java.class.path"));

You can do this with batch files in Windows NT/2000:
1) create a batch file called "setcp.bat" containing the following:
for %%i in (C:\myPath\*.jar) do call appendcp.bat %%i
2) create another batch file called "appendcp.bat" containing the following:
set CLASSPATH=%CLASSPATH%;%1
Now you can run setcp.bat and it will add all JAR files in C:\myPath to your CLASSPATH.
Jesper

Similar Messages

  • How to set classpath from java application

    in my ian using Runtine.exec();
    here the code
    String cmd[]={"cmd.exe","/c","path=C:\\j2sdk1.4.2_03\\bin\\;"};
              Runtime runtime = Runtime.getRuntime();
              Process setPathProc = runtime.exec(cmd);
              Process proc = runtime.exec("javac "+path+"\\"+clsname+".java");
    it working when ian running the application class from command, but when iam creating a jar file and running it was not working,,
    any suggestion

    hi kaj,
    my problem is ,
    In my application i have to compile one java file and
    create class file for that java file, here before
    creating i have to set class path, am i right?
    so in my program only first i have to set class path,
    and later i have to compile, so, in my code i have
    mentioned the details about to set class path and
    compile the program, r u getting what iam telling,
    please help me, if u know anything,
    thanks

  • Set cpu affinity within java application

    Hi,
    I have a problem running my java applications (100% pure java, jdk 1.4.1) on windows 2000 systems with more that 1 CPUs.
    The applications are Swing Java applications built using IBM Eclipse.
    When the applications start I can see the process running using Window Task Manager but I cannot see the Swing window.
    If I set, during the application start up, the process affinity to use just 1 CPU the application starts and runs without any problem.
    Anyone has any hit about this problem?

    There's a known issue with using multi-processor machines and Swing.
    Basically, due to the fact that Swing is non-reentrant.
    You can also get the problem is you have 2 different threads trying to
    alter the same component on a single processor machine.
    You might be able to minimise the problem by forcing your Swing code
    to queue up nicely on the Swing event queue as follows.
    final Runnable runnable = new Runnable()
       public void run()
           // any Swing code eg. panel.setColor ( Colour.blue );
       };    // runnable
    SwingUtilities.invokeLater(runnable);regards,
    Owen

  • Report Execution from within Java application

    I am currently running my Oracle reports through a Java application where I just create a HttpURLConnection to the reports server and push in my report request, and then read the connection input stream in order to obtain the generated report. This is working without too many problems except that in some cases where the report is generated but the application does not seem to understand the reponse.
    I see that there is in fact a Java API available for the reports. Is it possible to integrate this into my application in the same/similar way as I am doing now? If so, where can I get the associated docs as the supplied Javadocs are not too explicit.
    Thanks
    Justin

    You'll have to write a custom ClassLoader to do this - modifying the existing ClassPath from within the JVM just isn't possible.

  • How to set classpath from java class ??

    I have tried to use System.setProperty("java.class.path", "my class path string ") to set classpath dynamically. But it is not working. How to set it dynamically from java class ?? Thanks , gary

    Look into the java.net.URLClassLoader. You can't set the classpath after the fact but you can specify URL's that will checked when you try to load a class with that loader.

  • Set classpath from java code

    Hi,
    Is it possible to set new classpath from java code? Something like System.setClasspath(String);
    Many thanks
    Miso

    You aren't the first one to wonder whether that would work, so don't call yourself stupid on that account.
    Not taking 15 minutes to set up and run a test to actually see if it works... well, if you really needed the answer, that would be different.

  • Starting tomcat server after setting classpath for java

    Sorry if this is nothing to do with java, it is more to do with starting tomcat server.
    Tomcat server has been working totally fine, but when I wanted to compile a java servlet I wrote for it, I needed to run a BAT file called gojava in MS-DOS that had the classpath for java to work properly. I then attempted to run Tomcat, but just got 'bad command'. As long as I don't start up java by setting the classpath, tomcat will work, but as I need java to write servlets and then to test them without restarting my PC each time, how do I get tomcat to work?
    Thanks
    Hannah

    Sounds like your batch file is changing your path, not your classpath. If you're getting "Bad command or filename" when attempting to run Tomcat, it can't find the executable. Check out the .bat file and see what it's doing (maybe even post it here for further clarity). If it's changing your path at all, make sure it includes a %path% to indicate that it simply wants to append items to the path, not overwrite it completely. Sorry if i'm way off base with my suggestions, but it sounds like that's where the problem's going to be, especially if rebooting resolves the problem.

  • Set classpath for java in fedora core 1?

    i am new to programming java on linux and im having trouble finding out how to set classpath in the shell, permenently. i have the sdk installed and tried the usual "hello world" test and failed. the response i would get a message telling me javac is not a bash command. i have no clue how to set classpaths in linux so i would much appreciate it if someone would show me thanks

    Ok, there is two ways to achieve this. Either by setting it globally for all the user, or only for your self. If you are working on your own station and have super user access, you can set it up for all th eusers, if not, just set it for your self. How ??
    for all the users, modify the file /etc/profile and include this:
    PATH=$PATH:/path/to/jdk/bin:.
    CLASSPATH=$CLASSPATH:/path/to/jdk/lib/tools.jar:/path/to/jdk/lib/rt.jar
    export PATH CLASSPATH
    If you want to add it only for ur self then, modify the lsame line in ~/.bash_profile
    hope this helps

  • Executing a perl script from within java application

    Hi,
    Does anyone knows a way to execute a perl script from within java.
    ---kirk

    Runtime.exec("perl myscript.pl");
    Of course whether that "works" depends on what the script does and where the java program runs.

  • Add JARs to classpath within Java

    I've read through a number of different posts on this topic, but can't seem to get my code working.
    I want to write a class that will run embedded Tomcat. For certain reasons, I can't set the
    classpath to Tomcat before my class gets called, so I want to locate the Tomcat JARs, add them
    using a ClassLoader and then call the appropriate classes to setup and run Tomcat. An
    exception is being thrown, so I'm doing something wrong. Any ideas?
    Here's the code I'm trying to add the JARs with:
    String mcatHome = "tomcat/lib"
    try {
         // Scan folder for JARs and build URL array
         File mcatDir = new File(mcatHome);
         File files[] = mcatDir.listFiles(new JarFilter());
         URL jarURLs[] = new URL[files.length];
         for (int i = 0; i < files.length; i++)
              jarURLs[i] = files.toURL();
         // Add list of JARs to class loader
         URLClassLoader ucl = new URLClassLoader(jarURLs);
         // Use loader when loading classes
         Thread.currentThread().setContextClassLoader(ucl);
    catch (Exception ex) {
    then I call the contructor on the class which tries to start Tomcat:
         public void Mcat()
              startTomcat();
         protected void startTomcat()
              Embedded embed = new Embedded();
    When the constructor is invoked, I get:
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/catalina/L
    ifecycleException

    jschell, the list of JARs is generated from listing them in the folder. I did print the list out and
    the URLs looked ok. (i.e. file:/....)
    Here a snippet of how the class calling is setup:
    In Runner.java:
    public void Runner()
         //code from original post to load JARs
         Mcat mcat = new Mcat()
    In Mcat.java:
    import org.apache.catalina.*;
    import org.apache.catalina.logger.*;
    import org.apache.catalina.users.*;
    import org.apache.catalina.realm.*;
    import org.apache.catalina.startup.Embedded;
    public class Mcat
        public void Mcat()
            startTomcat();
        protected void startTomcat()
            Embedded embed = new Embedded();
    }Hope this helps

  • SETTING CLASSPATH N JAVA

    HI ,
    I AM LEARNING JAVA, ONE PROBLEM THAT I AM FACING IS HOW TO SET THE CLASSPATH SO THAT I CAN COMPILE AND RUN JAVA FILES PLACED IN DIFFERENT DRIVES OR FOLDERS. THE PROBLEM I HAVE NOW IS THAT I HAVE TO PLACE ALL MY .JAVA FILES IN THE BIN FOLDER IN ORDER TO RUN THEM.

    http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
    Javapedia: Classpath
    Setting the class path (Windows)
    How Classes are Found

  • Setting Classpath for java in UNIX machine

    Hi
    I need to work in JAVA in UNIX machine. But, I don't know how to set the classpath in the unix machine. If anyone knows, please let me know.

    setenv CLASSPATH .:/homes/code:${CLASSPATH}
    http://www.cise.ufl.edu/class/cop3530fa04/WinHowTo.htm

  • Invoking business rules 11g from Java application

    We have SOASuite 11g running on weblogic and have read through documentation/played with Oracle business rules
    We want to utilize business rules engine within our Java web applications deployed to glassfish server (Non SOA application). Is this even possible and does anyone use it in this way? Any pointer is much appreciated.
    What are the libraries like Oracle rules SDK that I need to include in the classpath of Java web app?
    This link assumes that web app is co-located in the same weblogic instance running SOASuite and doesn't help my case.
    http://docs.oracle.com/cd/E23943_01/user.1111/e10228/non_soa.htm#BGBBCICB
    By using it within Java application, we want to able to make local calls as opposed to calling a decision function as web service. Volume of actual rules and their usage is so high within our Java app. Also we want to take advantage of java beans defined in our business layer by using Java facts and without having to define XSD for XML facts.

    I had seen the blog, It explains how to test business rules by loading the dictionary from static rules file.
    my requirement is to invoke the business rule engine running on the SOA server, from the application module. can you please help me in this.
    Thanks inadvance.
    -Murali

  • HOW to play flash inside java application program

    Do java provide any API for playing flash inside java application ?Pleae give me some help about it .Thank You ..

    pingyi wrote:
    Do java provide any API for playing flash inside java application ?Pleae give me some help about it .Thank You ..bro... I found out there from the internet... but you need to purchase it.
    hope it meets your expectation...
    JFlashPlayer is a Flash Player API Java package that lets developers play and interact with Adobe? Flash Player movies within Java applications. Advanced animations can improve the user experience and add to the professional appearance of an application. JFlashPlayer allows programmers to quickly add Flash movies to their Java application development for Windows.taken from: JflashPlayer

  • Excel as a Component in java application

    Can anyone please tell me that how can I use an Excel worksheet as a COM object within Java application. please reply urgently

    look up www.tidestone.com, they had an MS Excel lib you could get for java. It is not free and they also just sold to another company. See if they still offer it.

Maybe you are looking for

  • Reducing form load in Adobe Reader / Optimizing Interactive Dynamic Forms

    I have a four page form that is taking approximately a minute to minute and a half on my T61P with 3 Gigs and Intel Pentium CoreDuo T7500 @ 2.2GHz. Everything loads up fine so it is only this pdf that is the problem. Can someone help me or point me t

  • Applications and Games look ZOOMED in

    I recently upgraded from an out 8900 Curve to a 9300 curve, and now when I try to run some of the applications and games I brought over (using the desktop manager) they are all zoomed in (or running in an incorrect resolution).  Please help! Thanks!

  • Please Help *Deadline Coming Up* Dropping Frames

    hello everyone. I am getting ready tired. I've burnt this project 9x already. I'm using iMovie 08 and I'm editing on a Macbook with an 500 gig firewire drive. The drive has 230 gigs left so it should have plenty to write a temp file for rendering. I

  • Microsoft Outlook Online Access

    I have finally gotten my phone to recieve emails from my Outlook Access online, however, now it seems as though my messages are delayed by anywhere from 5minutes to 30minutes.  Is there a setting I can change to get them quicker, or am I just SOL and

  • Can't get rid of the black

    http://dealeradbrowser.com/newsletters/august2013.html I can't get rid of the black under the guy. I can't find it in the code. No borders. No padding. Any suggestions? Thank you.