what does rt.jar do in java

rookie1010

Senior member
Mar 7, 2004
984
0
0
Hello

what does the following code do, my underastanding is that the rt.jar somehow invokes the jre but why have it?

C:\JDBCApp>javac -classpath .;c:/j2sdk1.4.2_04\jre\lib\rt.jar JDBCApp.java

C:\JDBCApp>javac -classpath .;c:/j2sdk1.4.2_04\jre\lib\rt.jar JDBCDemo.java

C:\JDBCApp>
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
JDBCApp.java and JDBCDemo.java use classes from rt.jar. Open it up and read the code - you should see import statements matching classes in the jar file.
 
Sep 29, 2004
18,656
67
91
Why not extract the jar to it's files, see what the files are, then look for the API?

You might need to DL the Java source ... but it's doable.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
rt.jar contains all of the compiled class files for the base Java Runtime ("rt") Environment. Normally, javac should know the path to this file; I've never seen a case where one had to specify it with a -classpath argument.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: IHateMyJob2004
Why not extract the jar to it's files, see what the files are, then look for the API?

You might need to DL the Java source ... but it's doable.
The class library source comes with the distribution. But MrChad already answered this properly anyways.
 

rookie1010

Senior member
Mar 7, 2004
984
0
0
thaks for the replies

rt.jar contains all of the compiled class files for the base Java Runtime ("rt") Environment. Normally, javac should know the path to this file; I've never seen a case where one had to specify it with a -classpath argument.

that is what i was wondering , if javac knows the path to the base runtime environment, then why specify it with the -classpath argument?

why is rt.jar in archive format, is it to conserve memory, does the run time envinroment, unarchive it and then extract the required class?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: rookie1010
that is what i was wondering , if javac knows the path to the base runtime environment, then why specify it with the -classpath argument?
You don't need to. Where did you get this example?
why is rt.jar in archive format, is it to conserve memory, does the run time envinroment, unarchive it and then extract the required class?
All jars are zip files. It's primarily for the sake of bundling many files into one but, yes, it does save disk space (certainly not memory, since it takes extra memory to unzip it). And 'yes' to the last question.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I don't know. Do mobile phones not use jars? I wasn't aware of it being an se standard only. I doubt it's significant given that one way or another, the classes in the jar still have to be kept in memory.
 

rookie1010

Senior member
Mar 7, 2004
984
0
0
thanks for the reply

i thought what you meant is that the classes n the jar bloat up when being extracted, and hence they need more memory when being used.

I think i understand what the reason for the rt.jar in the command line might be,

my JDBCDemo class code is



and it is not importing the required libraries which define Connection, Statement, Resultset.

I guess these are all javaclasses, if they are, is there any way to find which libraries they are located in, so that i may add the required libraries? is there any way i can add the rt.jar to JDBCDemo.java, or is that ot possible

i tried to compile JDBCDemo.java and it came up with the following
C:\JDBCApp>javac JDBCDemo.java
JDBCDemo.java:2: cannot resolve symbol
symbol : class Connection
location: class JDBCDemo
Connection db_connection;
^
JDBCDemo.java:3: cannot resolve symbol
symbol : class Statement
location: class JDBCDemo
Statement db_statement;
^
JDBCDemo.java:4: cannot resolve symbol
symbol : class ResultSet
location: class JDBCDemo
ResultSet result;
^
3 errors

C:\JDBCApp>
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Um, Connection, Statement and Result set are all in the base class libraries and you definitely don't need to add anything extra to the command line to use them.

I'm guessing you don't have "import java.sql.*;" in your JDBCDemo.java.
 

rookie1010

Senior member
Mar 7, 2004
984
0
0
yes, that works fine

thanks dude,i wonder why the author of the article missed this line of code out

is there any difference in the usage of the following statements

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;


&

import java.sql.*;

does one have a advantage over the other?
 

rookie1010

Senior member
Mar 7, 2004
984
0
0
hello

i tried to compile the following code



and am getting the following errors


C:\JDBCApp>javac jdbcapp.java
jdbcapp.java:13: cannot resolve symbol
symbol : method loadDriver ()
location: class JDBCDemo
dbInstance.loadDriver();
^
jdbcapp.java:14: cannot resolve symbol
symbol : method connectToDB ()
location: class JDBCDemo
dbInstance.connectToDB();
^
jdbcapp.java:15: cannot resolve symbol
symbol : method createStatement ()
location: class JDBCDemo
dbInstance.createStatement();
^
jdbcapp.java:16: cannot resolve symbol
symbol : method createTable ()
location: class JDBCDemo
dbInstance.createTable();
^
jdbcapp.java:17: cannot resolve symbol
symbol : method insertRecord (java.lang.String,java.lang.String)
location: class JDBCDemo
dbInstance.insertRecord("1", "John Doe");
^
jdbcapp.java:18: cannot resolve symbol
symbol : method insertRecord (java.lang.String,java.lang.String)
location: class JDBCDemo
dbInstance.insertRecord("2", "Jane Smith");
^
jdbcapp.java:19: cannot resolve symbol
symbol : method commitChanges ()
location: class JDBCDemo
dbInstance.commitChanges();
^
jdbcapp.java:20: cannot resolve symbol
symbol : method executeQuery ()
location: class JDBCDemo
dbInstance.executeQuery();
^
jdbcapp.java:21: cannot resolve symbol
symbol : method updateRecord ()
location: class JDBCDemo
dbInstance.updateRecord();
^
jdbcapp.java:22: cannot resolve symbol
symbol : method executeQuery ()
location: class JDBCDemo
dbInstance.executeQuery();
^
jdbcapp.java:23: cannot resolve symbol
symbol : method closeConnection ()
location: class JDBCDemo
dbInstance.closeConnection();
^
jdbcapp.java:46: cannot resolve symbol
symbol : variable DBConnection
location: class JDBCApp
DBConnection = DriverManager.getConnection (url, "dba", "sql");
^
jdbcapp.java:46: cannot resolve symbol
symbol : variable DriverManager
location: class JDBCApp
DBConnection = DriverManager.getConnection (url, "dba", "sql");
^
jdbcapp.java:60: cannot resolve symbol
symbol : variable DBStatement
location: class JDBCApp
DBStatement = DBConnection.createStatement();
^
jdbcapp.java:60: cannot resolve symbol
symbol : variable DBConnection
location: class JDBCApp
DBStatement = DBConnection.createStatement();
^
jdbcapp.java:75: cannot resolve symbol
symbol : variable DBStatement
location: class JDBCApp
DBStatement.executeUpdate(custTable);
^
jdbcapp.java:81: exit(int) in java.lang.System cannot be applied to ()
System.exit();
^
17 errors

C:\JDBCApp>

why am i getting the errors, all the methods, loadDriver, ... all seem to be part of the class all seem to be part of the class JDBCDemo which has been compiled and resides in the same directory as JDBCApp.

or should i some how include JDBCDemo in JDBCApp?

the code for JDBCDemo is

Code:
import java.sql.*;

public class JDBCDemo {
   Connection db_connection;
   Statement db_statement;
   ResultSet result;
}


or should i include the functions (loadDriver,..) as part of JDBCDemo, in which case i get the following errors

Code:
C:\>cd jdbcapp

C:\JDBCApp>javac jdbcdemo.java
jdbcdemo.java:30: cannot resolve symbol
symbol  : variable DBConnection
location: class JDBCDemo
      DBConnection = DriverManager.getConnection (url, "dba", "sql");
      ^
jdbcdemo.java:30: cannot resolve symbol
symbol  : variable DriverManager
location: class JDBCDemo
      DBConnection = DriverManager.getConnection (url, "dba", "sql");
                     ^
jdbcdemo.java:44: cannot resolve symbol
symbol  : variable DBStatement
location: class JDBCDemo
      DBStatement = DBConnection.createStatement();
      ^
jdbcdemo.java:44: cannot resolve symbol
symbol  : variable DBConnection
location: class JDBCDemo
      DBStatement = DBConnection.createStatement();
                    ^
jdbcdemo.java:59: cannot resolve symbol
symbol  : variable DBStatement
location: class JDBCDemo
      DBStatement.executeUpdate(custTable);
      ^
jdbcdemo.java:65: exit(int) in java.lang.System cannot be applied to ()
      System.exit();
            ^
6 errors

C:\JDBCApp>javac jdbcapp.java
jdbcapp.java:17: cannot resolve symbol
symbol  : method insertRecord (java.lang.String,java.lang.String)
location: class JDBCDemo
      dbInstance.insertRecord("1", "John Doe");
                ^
jdbcapp.java:18: cannot resolve symbol
symbol  : method insertRecord (java.lang.String,java.lang.String)
location: class JDBCDemo
      dbInstance.insertRecord("2", "Jane Smith");
                ^
jdbcapp.java:19: cannot resolve symbol
symbol  : method commitChanges ()
location: class JDBCDemo
      dbInstance.commitChanges();
                ^
jdbcapp.java:20: cannot resolve symbol
symbol  : method executeQuery ()
location: class JDBCDemo
      dbInstance.executeQuery();
                ^
jdbcapp.java:21: cannot resolve symbol
symbol  : method updateRecord ()
location: class JDBCDemo
      dbInstance.updateRecord();
                ^
jdbcapp.java:22: cannot resolve symbol
symbol  : method executeQuery ()
location: class JDBCDemo
      dbInstance.executeQuery();
                ^
jdbcapp.java:23: cannot resolve symbol
symbol  : method closeConnection ()
location: class JDBCDemo
      dbInstance.closeConnection();
                ^
.\JDBCDemo.java:30: cannot resolve symbol
symbol  : variable DBConnection
location: class JDBCDemo
      DBConnection = DriverManager.getConnection (url, "dba", "sql");
      ^
.\JDBCDemo.java:30: cannot resolve symbol
symbol  : variable DriverManager
location: class JDBCDemo
      DBConnection = DriverManager.getConnection (url, "dba", "sql");
                     ^
.\JDBCDemo.java:44: cannot resolve symbol
symbol  : variable DBStatement
location: class JDBCDemo
      DBStatement = DBConnection.createStatement();
      ^
.\JDBCDemo.java:44: cannot resolve symbol
symbol  : variable DBConnection
location: class JDBCDemo
      DBStatement = DBConnection.createStatement();
                    ^
.\JDBCDemo.java:59: cannot resolve symbol
symbol  : variable DBStatement
location: class JDBCDemo
      DBStatement.executeUpdate(custTable);
      ^
.\JDBCDemo.java:65: exit(int) in java.lang.System cannot be applied to ()
      System.exit();
            ^
13 errors

C:\JDBCApp>
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: rookie1010
is there any difference in the usage of the following statements

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;


&

import java.sql.*;
The second version is more compact and easy to write but it's also more susceptible to annoying problems if you happen to want to use a completely different class from another package that haa the same name as a class in java.sql. For example, java.sql.Date and java.util.Date.

I tend to use the first way because Eclipse takes care of setting up and sorting the list, collapses it so I don't have to see it and warns me if I have unused imports.
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
I don't know how it ranks up specifically with J2ME, but if what you're using right now is requiring this much trouble, I'm sure Eclipse will be better than it.