Using JAR's

Hi,
i have a large application spread across multiple files. Since the app is to be run from a central server, i would like to put all the class files into 1 jar file. i have managed to create the jar file and it works. however, i get a "cannot find the driver classes" error when i try to access the database.
this is the code that fails:
private String driverName="com.sybase.jdbc2.jdbc.SybDriver";
Connection conn;
Statement stmt;
ResultSet rst;
try{
     Class.forName(driverName);
     //some database stuff
catch(ClassNotFoundException e)
     System.err.println("Cannot find driver classes");
catch(SQLException ex)
     System.err.println("SQL EXception\n" + ex);
its failing at the class.ForName part. how can i fix this? do i need to change the path when using a JAR file? do i need to explicitly set CLASSPATH?

This is not a problem with your Jar file.
It is your command not completed.
There are two ways to do this.
The easy way, find the main running class in your MyJarFile.jar and do this:
java yourPackageName.YourMainRunningClass
(since all the necessary files are in your classpath already)
The long typing way, do this:
java -classpath repeatYourFullClasspathHere -jar MyJarFile.jar
--lichu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Error while using .jar file instead of class file.

    Hello friends,
    I created a java class which are referred and called from other jsp\xml and java files. I compiled this java class and everything is working properly. However, I do not want to use class files but want to use jar files instead. So, I created a jar file of this class file by using the command "jar -cvf jarfilename.jar".I kept the jar file in its respective folder .Also, I removed the class file from its path, just to see that the jar file will work automatically.
    Unfortunately, I am getting an error:
    java.lang.RuntimeException: Component.createComponent(...) Component class not found: <java class path>. I also tried giving the path of this jar file in the classpath. This also does not seem working.
    How to solve this problem?
    Thanks,
    Ranjith M.V
    Edited by: RanjithM.V on May 11, 2008 10:34 PM

    You probably populated the jar file incorrectly.
    A common mistake is to include the path that you had on the classpath, in the jar file.
    So a person will have a classpath like this: /foo/
    And a class in a package bar, like this:
    package bar;So that other classes would include the file like this:
    import bar.TheClass;And the class file is on the filesystem like this:
    .../foo/bar/TheClass.classThen they'll create the jar file including the foo directory, so if they list the jar's contents, they'll see the above (/foo/bar/TheClass.class). But this is wrong. It should start off so that the jar entries start with the top-level package names (like /bar/TheClass.class).
    Maybe you've done that.

  • Help using jar file!

    Help using jar file!
    Hello
    I have created a jar file by using
    jar cvmf Manifest.txt myjar.jar 1.class Mydirectory
    In 1.java file :I have used
    JEditorPane editor;
    editor.setPage(getClass().getResource("/Mydirectory/default.htm"));
    If I am only giving myjar.jar file to the client without Mydirectory then also it is working fine by showing default.htm in JeditorPane.
    But my problem is I want to use
    Runtime.getRuntime().exec("cmd /c start IEXPLORE"+targetstr) ;
    targetstr will be the filename with path of the default.htm
    If I am giving myjar.jar file with Mydirectory to the client it is working but I don't want to give Mydirectory to the client .
    What should I do?
    Is there any solution for this?
    Using another jar file which will include mydirectory can solve this problem?
    Is there any othe concept in java so that I wll be able to hide Mydirectory from client?
    Please help.

    It seems like you could extract the .htm file from the jar, either with Runtime.exec or using the Jar API classes.

  • Creating & Using .jar files and manifests

    I've read through hundreds of online tutorials on creating and using .jar's, but it's surprising how unhelpful they really are.
    I've been trying to make a java program of mine standalone, so I can distribute it to others and allow them to run it without use of a compiler, etc (ideally just a .exe or .bat that they double click on). Everything I've found says I need to create a .jar of my program in order to do this. I was finally able to create a .jar but I got different errors relating to "main-class" and "manifest" issues...I tried running it with javaw.exe and had the same problem.
    Some tutorials briefly discuss "meta manifest's", other's skip the concept all together in creating .jar's. I created a jar with:
    jar cf A2.jar c:\jarTester.java
    after changing my command prompt path to
    C:\Program Files\Java\jdk1.6.0_01\bin
    It created the .jar, but nothing worked. As above, it gave me errors relating to the "Main-Class" and "Manifest". So here's my question:
    What involvement does a "manifest" play in .jar's, and how do I create/use one to create a .jar? Why is it necessary to specify the "Main-Class" when I have already done so in my .java file?
    I believe that once I can get this "Main-Class" and "Manifest" problem straightened out, I will be able to create a batch file with the line:
    java -jar A2.jar
    is this correct? THANKS.

    I've read through hundreds of online tutorials on
    creating and using .jar's, but it's surprising how
    unhelpful they really are.really, hundreds?
    I've been trying to make a java program of mine
    standalone, so I can distribute it to others and
    allow them to run it without use of a compiler, etc
    (ideally just a .exe or .bat that they double click
    on). Everything I've found says I need to create a
    .jar of my program in order to do this. Make up your mind, do you want to make a jar or an exe?
    I was
    finally able to create a .jar but I got different
    errors relating to "main-class" and "manifest"
    issues...I tried running it with javaw.exe and had
    the same problem.
    Some tutorials briefly discuss "meta manifest's",
    other's skip the concept all togetherthat's because the manifest isn't usually necessary
    in creating
    .jar's. I created a jar with:
    jar cf A2.jar c:\jarTester.javaugh, that won't work. you need to put the class file in there, not the source. or you can put the source in as well if you want, but the class is necessary
    i'm ignoring the rest of this post for now
    go back to Google and type in jar manifest and read more

  • Using .jar files

    I was able to use .jar files in Java programs that I write on my computer. I could write a program that used an import statement to bring in classes contained in a .jar file.
    One day, I was trying to incorporate a jar file folder directly into my windows operating system by using the environmental variable called CLASSPATH. Unfortunately, I made the mistake of screwing up my PATH variable. I fixed my PATH variable back up by adding the directory to the bin folder inside of the Java folder as is described commonly by the documentation for installing the Java SE on windows computer.
    Since that day, I have not been able to use .jar files using an import statement. My computer is unable to find the .jar files even if I specify the exact directory. Even the IDE(netbeans) that I use is unable to find and use .jar files. I have found that I can open up .jar files and pull the contents that I need out of said .jar files, but I have a project that does not allow me to open up the .jar files and extract the needed classes.
    I have googled this problem so much that I have read hundreds of posts about .jar files, and I cant find anyone in a situation that is similar to mine. If I take my files to another computer with Java SE installed, then my source code can access the .jar files just fine. For some reason, my work computer seems to hate using .jar files. I have no idea what could be the problem. There is no information in the java documentation on troubleshooting with this problem, and the most confusing part is that it worked perfectly fine until That one day
    Does anyone have any ideas?
    Specific technical details:
    I need to pull the Stopwatch.class file from a stdlib.jar file. In the past I had done this by using an import statement inside of my program:
    "import stdlib.Stopwatch; "
    Using this command to compile:
    "javac -cp .;(directory path to .jar file); source.java"
    This used to work. Now when using the javac command I get error messages
    1. Error points directly to import statement. Arrow directed at the period after stdlib.
    "cannot find symbol"
    2. Error message pointing to all instances of the class that I was attempting to import into my program.
    Thanks for reading
    Edited by: 955611 on Aug 28, 2012 7:22 PM

    I read your post. I want to compile this project by using the "-classpath" option in the javac command. After screwing up important system variables once, I do not want to continually have to play with system variable in order to compile projects. To quote from the website that you linked:
    Using the JDK tools' -classpath option
    The JDK tools java, jdb, javac, and javah have a -classpath option which replaces the path or paths specified by the CLASSPATH environment variable while the tool runs. This is the recommended option for changing class path settings, because each application can have the class path it needs without interfering with any other application.
    The runtime tool java has a -cp option, as well. This option is an abbreviation for -classpath.
    For very special cases, both java and javac have options that let you change the path they use to find their own class libraries. The vast majority of users will never to need to use those options, however.
    Clearly, the "-classpath" option in the javac is intended to replace the need to play with important system variables. I guess my post boils down to the question of "Why doesn't this command work for me?". I followed your suggestion by removing the bin directories and adding the stdlib.jar directory directly into the system variable itself. Here is the following output from my command prompt:
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    C:\Documents and Settings\Lelo>E:
    E:\>cd Programs\Java
    E:\Programs\Java>javac testy.java
    testy.java:1: error: package stdlib does not exist
    import stdlib.Stopwatch;
    ^
    1 error
    E:\Programs\Java>echo %CLASSPATH%
    C:\Program Files\Java\jre6\lib\ext\QTJava.zip;E:\Programs\Java\stdlib.jar;
    E:\Programs\Java>javac -classpath .;E:\Programs\Java\stdlib.jar; testy.java
    testy.java:1: error: package stdlib does not exist
    import stdlib.Stopwatch;
    ^
    1 error
    E:\Programs\Java>
    I still received the same errors. It seems that even adding the directories into the system variable will not work for me.

  • Using JAR file in running application and POI !

    Hi all
    I am trying to use POI HSSF f(for the first time, So pls bear with me!)or creating excel file from my java Application. I am using the jar file :
    poi-2.0-RC2-20040102.jar. Not sure if its the correct one. So If I am wrong pls correct me.
    Now I am creating a bat file to compile my application:
    @echo off
    C:\jdk1.3.1_04\bin\javac -classpath C:\POI\poi-2.0-RC2-20040102.jar MyApp.java
    pauseIt compiles fine. Now If I try to use the jar file in run.bat for running my application, it says NoClassDef FoundError:MyApp. Here is my run.bat
    java -classpath C:\POI\poi-2.0-RC2-20040102.jar MyApp
    pauseI am not able to run my application with this run.bat file . If I change the content to:
    "java MyApp.java"
    It runs but give me error: NOClassDefFoundError:import org.apache.poi.hssf.usermodel.HSSFWorkbook
    Pls suggest me about how to use jar file in running my app and how to rresolve workbook finding problem.
    Reagrds

    Try this:
    java -classpath .;C:\POI\poi-2.0-RC2-20040102.jar MyApp
    pauseNote the "dot semi-colon" at the start of the CLASSPATH. That puts the current directory in the CLASSPATH, which is where MyApp.class lives. (No package, right?)
    See if that works better.

  • Problem using Jar to include external jars in an application

    Ok so, I've just completed a project, and i want to be able to jar it up so its executable by just using the jar file. I can do this, and it will load the first screen. However, i used eclipse to develop it so I just added two files to the eclipse path and it all worked. But when I jar up my application (and include the two external jars in my app), and try to use any class that needs the external jar, it dies and says java.lang.NoClassDefinitionError: javax/servlet/http/HttpServlet
    How do i set it up so it actually finds the classes in the external jar?? Its fairly important that i get this to work :)

    If this app is not running as part of Tomcat, you have two options. One, as ejp pointed out, is to put the missing jars into your $JAVA_HOME/lib/ext dir. If you've already tried that and it's not working, then the jars your putting there do not have the necessary classes in them. Use 'jar -tf <jarname>' to see a list of the classes in the jar.
    The second approach, which it sounds like you've already tried, is using the Class-Path entry in the Manifest file. You must make sure that the jar you're referencing (servlet.jar) has the missing classes in it, and that the jar is in the same directory as the application jar. Another potential gotcha with this approach is to make sure that the jar is not indexed (they're not indexed by default).

  • Using JARs with JSP?

    How do you include JARs in a classpath so that the classes inside can be referred to in <%@ page import="mypackageinajar.myclassinajar" %> ? I've looked through the JSP docs but it only covered import statements. If I can't use a JAR, how would I enable my program to access the class files that I could just manually extract from the JAR?
    Thanks

    How do you include JARs in a classpath so that the
    classes inside can be referred to in <%@ page
    import="mypackageinajar.myclassinajar" %> ? I've
    looked through the JSP docs but it only covered
    import statements. If I can't use a JAR, how would I
    enable my program to access the class files that I
    could just manually extract from the JAR?You can use JARs, but this is simply a question of how to pack your stuff into a WAR. It has nothing really to do with JSPs...
    http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html

  • Using .jar files in WebDynpro DC's

    Hello,
    I am having a problem while trying to create a structure for a Web Dynpro component using .jars.
    For a better understanding of the background, the whole story happens in JDI SS9 (SAP NW Dev Studio 2.0.9 and it's corresponding DTR version) therefore the External Library DC is not available to me.
    That being said, here goes the problem description.
    We would like to create a WebDynpro DC (DC1) which would contain a WebDynpro component WDA using a WebDynpro component WDB which has been developed as part of another DC DC2.
    Now, of course that a typical DC structure (have both components inside DTR, create the appropriate dependencies between them, so on, so forth, will work). But there is another thing we would like to do, namely use the .jar created as a public part for WDB in DC2 directly.
    So, we include the named .jar as an external library in the project structure of WDB.
    Let aside runtime issues, which are going to be solved by using sharing references, DC1 won't compile - moreover, because WDB cannot be used as a 'Used WD component' from within WDA because it's not available at all. As if the .jar is not seen.
    But if we would create a simple project, not a DC, the .jar is perfectly usable that way. The WebDynpro project compiles fine, and WDB appears as a referenceable component.
    So, in short, the question is: what can we do to get a workable DC structure based merely on the .jar public parts and not on the whole DC tree ? A good example here would be the ability to create a Local DC using .jar public parts of DC's located in DTR.
    I repeat, the problem must be solved in the SS9 context. I am aware of the existence of External Libraries in later versions, but currently I find myself confined to this one.
    Thanks,
    Marius

    Hi Marius,
    check the following web blog,
    /people/valery.silaev/blog/2005/09/14/a-bit-of-impractical-scripting-for-web-dynpro
    thanks and regards
    Brahmanandam Ausali

  • Performance issue if we use jar file instead of classes

    Hi,
    My application uses tomcat as web server.
    If i use calsses in webapps -> WEB-INF -> classes folder, i place classes in that ,
    In other case i use jar file and place that file in WEB-INF -> lib folder in the webapps directory.
    There is huge performance difference.
    While using classes performance is great while using jar file performance is very disappointed.
    I am using a file for encryption /decryption also.

    I can't really believe that classes vs jars makes a difference, but whatever.

  • How to use jar files without setting classpath

    Hi,
    I have a situvation, I can not set classpath, but i have to use jar files, how can I do that.

    URL[] urls = new URL[]{pathToJar, pathToAnotherJar, ...};
    URLClassLoader urlc = new URLClassLoader(urls);
    Now load classes within the jars loaded by the urlc classloader as needed. This is a limited approach, you can mostly use interfaces to work with classes loaded by the custom loader instance above and your existing classes.

  • Use jar file

    dear guys,
    can somebody tell me why would use jar file? how it would be use? i would like to get the idea of jar file
    thanks

    Are you familiar with zip files? A jar file is just a kind of zip (the jar utility will even read zip files as though they were jars).
    The most common use is to hold .class files generated from all the classes of a Java program so that you can run the program from that one file, rather than very large numbers of small class files. Libraries of classes are almost always stored as jars.
    Of course you can put other stuff in there too, like image files and the like.

  • Hide classes while using jar tool.

    I have a set of API classes which I need to ship using jar.
    But even if I use jar utility to archive the classes, one can easily extract and decompile the classes.
    Is there any way this can be avoided?
    Regards

    Not really. You can use an obfuscator to make the code harder to decompile in a way that's easy to read. I'm told some obfuscators can generate class file code that breaks decompilers, but the code could still be disassembled or the next version of decompilers will be able to defeat it.
    By the way, this is an issue with any language.

  • How to use jar -C programmatically

    Hi,
    I am creating a jar file programmatically.
    I want to use jar -C option.
    e.g jar -cvf myjar.jar -C anotherDir .
    Can anybody tell me how to do it?

    Hi,
    I am creating a jar file programmatically.
    I want to use jar -C option.
    e.g jar -cvf myjar.jar -C anotherDir .
    Can anybody tell me how to do it?Why not offer dukes man :-)
    Sq2000

  • Can new folder be added into a war using jar commands?

    Hi All,
    I am working on an installer creation attempt to make some changes on a war at the client side to inject my changes into it. As a result i am in need to modify the war using jar commands and have found some success.
    I user the following command.
    D:\MyWarLab>jar uf webtop.war customThis command helped me in adding my changes inside a folder named 'custom' placed at location 'D:\MyWarLab' into the 'custom' folder inside my 'webtop.war'.
    Now this worked as expected since the 'custom' folder was already present inside the war.
    Now my next step is to inject a new folder named 'customA' inside
    webtop.war ->WEB-INF -> classes -> com -> application. This customA folder would contain the java class files i have created.The customA folder does not already exist at the above mentioned location, hence the need to create it.
    I have not been able to find a jar command to add new folders at desired locations of the war.
    Could you please validate my attempt and suggest a command to fulfil my requirement.

    Now this worked as expected since the 'custom' folder was already present inside the war.No. It worked because 'custom' existed in the file system as a directory with files under it. Whether it was already in the war is irrelevant.
    This customA folder would contain the java class files i have created. The customA folder does not already exist at the above mentioned location, hence the need to create it.If it exists on the disk you can add it to the war with the jar uf command.
    I have not been able to find a jar command to add new folders at desired locations of the war.You've already found it!

  • How to create java archive using jar command

    hello
    what is the syntax for creating java acrchive using jar command line
    jar -c ???????
    urgent plzz

    jar cf myjar.jar classfiles...
    If you have a manifest file you want to add:
    jar cfm myjar.jar manifest.mf classfiles...
    Also check the documentation:
    http://java.sun.com/j2se/1.3/docs/tooldocs/tools.html
    You can find a link to jar there.

Maybe you are looking for

  • WRT54GS V6 access question.

    Just set up the network. WRT54GS with a Wireless-G USB adapter 2nd pc is linked and working great! Question is: How do I access the 2nd pc from the 1st pc? I click on my computer > network places > view computer work groups. When I click on the 2nd p

  • Video editing/exporting in Photoshop CS6 - original quality gone in YouTube. Idea?

    Hello, i've searched this forum and other sources already, but couldn't find a solution. Unfortunately, i am not a video pro. I record video tutorials about Photoshop (so, screen captures) and edit/combine the clips inside Photoshop CS6 in Win 7/64;

  • I can't open my pdfs in the normal way

    I can't open a pdf by double clicking on it. I get an error message as follows: "The Document 'Blank page for email.pdf' could not be opened. Acrobat cannot open files in the 'Adobe PDF document' format". This started yesterday after I downloaded a d

  • Accessing ApplicationModule in Struts/JSP application using JDev 11g TP3

    Hello All, I am using Oracle JDeveloper 11g Technical Preview 3 I got one "AppModule" created under my (Struts/jsp) project "MODEL" structure. Now, how can I access that ApplicationModule in my Action Class BC4JContext bc4jContext = BC4JContext.getCo

  • Next month 7th day data

    01-jan-09 5 02-jan-09 3 03-jan-09 2 01-feb-09 1 02-feb-09 1 03-feb-09 1 04-feb-09 1 05-feb-09 1 06-feb-09 1 07-feb-09 1 select sum(featured_moveouts) OVER (PARTITION BY propertynumber ORDER BY relavantdate RANGE BETWEEN ADD_MONTHS (TRUNC ( relavantda