Results 1 to 2 of 2
  1. #1
    New-to-this is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default help calling 1 .java from another.

    It's late and I am tired...

    I cannot get this right, please help. I am using Eclipse 3.5.0 and JRE 6
    C:\Program Files\Java\jre6\bin\client\jvm.dll
    Classpath is
    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="C:/Documents and Settings/Phil/workspace/google/src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="C:/eclipse/testng-5.10-jdk14.jar"/>
    <classpathentry kind="lib" path="C:/eclipse/testng-5.10-jdk15.jar"/>
    <classpathentry kind="lib" path="C:/Selenium-remote-control-1.0.1/selenium-remote-control-1.0.1/selenium-java-client-driver-1.0.1/selenium-java-client-driver-test-sources.jar"/>
    <classpathentry kind="lib" path="C:/Selenium-remote-control-1.0.1/selenium-remote-control-1.0.1/selenium-java-client-driver-1.0.1/selenium-java-client-driver.jar"/>
    <classpathentry kind="lib" path="C:/Selenium-remote-control-1.0.1/selenium-remote-control-1.0.1/selenium-java-client-driver-1.0.1/selenium-java-client-driver-sources.jar"/>
    <classpathentry kind="lib" path="C:/Selenium-remote-control-1.0.1/selenium-remote-control-1.0.1/selenium-java-client-driver-1.0.1/selenium-java-client-driver-tests.jar"/>
    <classpathentry kind="output" path="bin"/>
    </classpath>


    all my .java is in C:/Documents and Settings/Phil/workspace/google/src

    this is my first program....

    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Label;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.io.IOException;


    public class UserForm extends Applet {
    String strrecordnolow;
    String strrecordnohigh;
    TextField txtrecordnolow = new TextField();
    TextField txtrecordnohigh = new TextField();
    Label lblrecordnolow = new Label();
    Label lblrecordnohigh = new Label();
    Button btnOK = new Button();
    Button btnCancel = new Button();

    //Construct the applet

    public UserForm()
    {

    }

    //Initialize the applet

    public void init() {
    try {
    jbInit();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void jbInit() throws Exception {
    lblrecordnolow.setBounds(new Rectangle(21,13,118,25));
    lblrecordnohigh.setBounds(new Rectangle(21,38,118,25));
    lblrecordnolow.setText ("From:");
    lblrecordnohigh.setText ("To:");
    btnOK.setBounds(new Rectangle(81, 219, 100, 40));
    btnOK.setLabel("OK");
    btnOK.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    btnOK_actionPerformed(e);
    }
    });
    btnCancel.setBounds(new Rectangle(224, 217, 100, 41));
    btnCancel.setLabel("Cancel");

    btnCancel.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    btnCancel_actionPerformed(e);
    }
    });

    this.setLayout(null);
    this.setBackground(Color.lightGray);
    this.setSize(new Dimension(400, 275));
    txtrecordnolow.setBounds(new Rectangle(154, 12, 219, 23));
    txtrecordnohigh.setBounds(new Rectangle(154, 35, 219, 23));
    this.add(txtrecordnolow, null);
    this.add(txtrecordnohigh, null);
    this.add(lblrecordnolow, null);
    this.add(lblrecordnohigh, null);
    this.add(btnOK, null);
    this.add(btnCancel, null);

    }

    void btnOK_actionPerformed(ActionEvent e) {
    strrecordnolow = txtrecordnolow.getText();
    strrecordnohigh = txtrecordnohigh.getText();
    System.out.println(strrecordnolow);
    System.out.println(strrecordnohigh);
    try {
    Runtime.getRuntime().exec( "ReadData.java" );

    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    }

    void btnCancel_actionPerformed(ActionEvent e) {
    txtrecordnolow.setText ("");
    txtrecordnohigh.setText ("");
    }


    }

    And this is my second...

    import java.sql.*;

    public class ReadData<New> {



    static Connection con;
    static Statement stmt;
    static ResultSet dataread;
    static int testref;
    static int recordnohigh;
    static int recordnolow;
    static int recordnocurrent;

    public static void main(String[] args) throws java.lang.ClassNotFoundException {

    UserForm UserForm = new UserForm();
    String newvariable = UserForm.strrecordnolow;
    System.out.println (newvariable);

    //step 1: load driver
    loadDriver();

    //step 3: establish connection
    makeConnection();

    //retrieve data
    retrieveData();

    //close all resources
    closeAll();
    }

    // load a driver
    static void loadDriver() {
    try {
    //step 2: Define connection URL
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    } catch(java.lang.ClassNotFoundException e) {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    }
    }

    // make a connection step 3: establish connection
    static void makeConnection() {
    //for how to set up data source name see below.
    String dsn = "PhilsDataSource";
    String url = "jdbc:odbc:" + dsn;
    try {
    con = DriverManager.getConnection(url, "", "");
    }catch(SQLException ex) {
    System.err.println("database connection: " + ex.getMessage());
    }
    }

    private static void println(String string) {
    // TODO Auto-generated method stub

    }

    //retrieve data from table Users
    static void retrieveData() {
    try {//try begins
    recordnolow = 1;
    recordnohigh =5;
    recordnocurrent = recordnolow;
    //Read starts at lowest figure in range and increments up to highest in range
    //All data read between these points
    do {
    String getdata="SELECT * FROM Users " + "WHERE TestRef = " + recordnocurrent;
    //process the results.
    stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE, ResultSet.CONCUR_UPDATABLE);
    dataread = stmt.executeQuery(getdata);
    // System.out.println (getdata);uncomment this line for Select statement info
    dataread.next();
    //Reads all fields in dbase - specifiy further ones here
    testref = dataread.getInt("TestRef");
    String Firstname = dataread.getString("Firstname");
    String Surname = dataread.getString("Surname");
    System.out.println(testref + ". " + Firstname + " " + Surname);
    recordnolow++;
    recordnocurrent = recordnolow;
    }while (recordnolow <= recordnohigh);

    }//try ends
    catch(SQLException ex)
    {//catch begins
    System.err.println("RetrieveData: " + ex.getMessage());
    }//catch ends

    }//static void retrieve data ends here




    //close statement and connection
    //step 7: close connection, etc.
    static void closeAll() {
    try {
    stmt.close();
    con.close();
    } catch(SQLException ex) {
    System.err.println("closeAll: " + ex.getMessage());
    }
    }
    }


    All I need to be able to do is call the ReadData.java from UserForm.java and pass the two variables recordnolow and recordnohigh through to ReadData.java, I then want to use these two variables to form the basis of my SQL selection in ReadData.java. I have managed to make the two scripts work independantly of each other but I cannot get them talking. When I run them through Eclipse to execute, my UserForm works right up until :

    try {
    Runtime.getRuntime().exec( "ReadData.java" );

    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    [/COLOR]

    Where it crashes with the error

    java.io.IOException: Cannot run program "ReadData.java": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at UserForm.btnOK_actionPerformed(UserForm.java:81)
    at UserForm$1.actionPerformed(UserForm.java:49)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 17 more



    AAAAAAAAAAAHHHHHHHHHHHH!!!!!

    I have two issues,
    1) The crash mentioned above - why can the system not find the file ReadData.java?
    2) How to move the two variables across from UserForm to ReadData, I am guessing - am I on the right lines with
    UserForm UserForm = new UserForm();
    String newvariable = UserForm.strrecordnolow;
    System.out.println (newvariable);


    thanks for the help folks. Sorry for the long post.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please use the dark color when you post something again here in the forum. The color you've used is really hard to look at. And also please use code tags when posting code snippets here in the forum. Formatted codes are easy to read.

    Java Code:
    Runtime.getRuntime().exec( "ReadData.java" );
    You cannot do this, how can you run a *.java file this way. Just think about in simple way. *.java file contain the source code. Can you run it, no. Why? Because the computer cannot understand those text and all in the file. What you have to do? Compile the source file and create the *.class file. It's understandable by the computer. Anyway, you cannot run a *.class file as well in this way. Did you read the Java doc about the Runtime class?

    And also, read bit more about objects in Java and member access. It'll help you find the answer in second topic you mention. Actually there are no two things.

Similar Threads

  1. Calling java functions from c++
    By khajalid in forum New To Java
    Replies: 13
    Last Post: 10-12-2010, 11:50 PM
  2. Calling a variable from a different java class
    By jdiesel in forum New To Java
    Replies: 4
    Last Post: 01-09-2009, 02:12 AM
  3. calling java virtual machine
    By many in forum New To Java
    Replies: 5
    Last Post: 12-01-2008, 11:06 PM
  4. calling a java application from another
    By kratoras in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:54 AM
  5. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •