Java Packages and Imports

Hi,
I have 2 java classes
A.java
B.java
### A.java ###
import B;
public class A
public static void main(String[] args)
public class B
public void foo()
when I compile B , it works fine, but when i compile A.
it gives following error
'.' expected
why java doesn't allow to create & import a class without package, is there any workaround to resolve this issue
this is a sample code i have posted, but actually I am facing the same problem in my current project. even if i include that class in classpath error doesn't go away.
Please suggest what can be done to resolve this issue.
Thanks in advance.
NP

- the default package (the package with no name)It does? Not for me.Gosh, you're right, it doesn't. Sorry.
Actually, I never use it, my post was a copy/past from damned tutorial...
- Tim - Harakiri - aaarrhhggg...

Similar Messages

  • How to create package and import from jar file?

    Hi all,
    I am writing a software and I am not sure how to create a package for the classes.
    Say I have two classes in the same directory as follows:
    testA.java
    ==========
    package AB;
    public class testA
    public static void main(String[] args){
         testB myB = new testB();
         System.out.println("A test");
    testB.java
    ===========
    package AB;
    public class testB
    public testB(){
         System.out.println("B constructor");
    both file compile without the package heading;
    both file compile using: javac -classpath .\ *.java
    Question 1:
    I cannot run testA by: java -classpath .\ testA
    I think it is a syntax error. What is the correct one?
    If I run testA by: java testA
    The only output I get is: A test
    But I am expecting: B constructor /n A test
    What went wrong?
    Question 2:
    I need to use APIs of another software. I have downloaded a .jar file (xxx.jar) with all the classes in it. And I have put "import xxx.*;" in my source file. But the compiler complains about the importing. What is the right way to copmile it?
    I have read a couple of tutorials but they don't answer my question.
    (I am using windows2000 and don't have the classpath variable.)
    Hope some one can help.
    Thanks a lot

    Try moving testA out of the package and importing 'AB.*;'
    If you have:
    ./testA.class
    ./AB/testb.class
    Then to execute testA from ./ type: java -cp . testA

  • JAVA PACKAGES and JSP

    i Have two problems one is i am designing a website which consists of a form. I am trying to write the Connectiviy and all other insert statements in seperate java packages and include them in JSP files.
    i Have two java packages one is DBUtil(having two java files both dealing with connectovity) and another one is StudentUtIl which has java files that open the connections with database connection and insert data into DB., I am having a big Problem here. How can i call the function in the other java package in the files in this java package. i have openDB()and Close() in the other Db and i want to call these functions in JAVA classes in the other package. How to include a package. Both these packages are in the same File system mounted on a Local directory

    Thank you for the reply. I am giving the code i have given the full class name . and now it is giving the following error :
    Cannot reference a non-static method connectDB() in a static context.
    I am also giving the code. Please do help me on this. i am a beginner in java.
    import java.sql.*;
    import java.util.*;
    import DButil.*;
    public class StudentManager {
    /** Creates a new instance of StudentManager */
    public StudentManager() {
    Connection conn = null;
    Statement cs = null;
    public Vector getStudent(){
    try{
    dbutil.connectDB();
    String Query = "Select St_Record, St_L_Name, St_F_Name, St_Major, St_Email_Address, St_SSN, Date, St_Company, St_Designation";
    cs = conn.createStatement();
    java.sql.ResultSet rs = cs.executeQuery(Query);
    Vector Studentvector = new Vector();
    while(rs.next()){
    Studentinfo Student = new Studentinfo();
    Student.setSt_Record(rs.getInt("St_Record"));
    Student.setSt_L_Name(rs.getString("St_L_Name"));
    Student.setSt_F_Name(rs.getString("St_F_Name"));
    Student.setSt_Major(rs.getString("St_Major"));
    Student.setSt_Email_Address(rs.getString("St_Email_Address"));
    Student.setSt_Company(rs.getString("St_Company"));
    Student.setSt_Designation(rs.getString("St_Designation"));
    Student.setDate(rs.getInt("Date"));
    Studentvector.add(Student);
    if( cs != null)
    cs.close();
    if( conn != null && !conn.isClosed())
    conn.close();
    return Studentvector;
    }catch(Exception ignore){
    return null;
    }finally {
    dbutil.closeDB();
    import java.sql.*;
    import java.util.*;
    public class dbutil {
    /** Creates a new instance of dbutil */
    public dbutil() {
    Connection conn;
    public void connectDB(){
    conn = ConnectionManager.getConnection();
    public void closeDB(){
    try{
    if(conn != null && !conn.isClosed())
    conn.close();
    }catch(Exception excep){
    The main error is occuring at the following lines connectDB() and closeDB() in the class student manager. The class dbutil is in an another package.with an another file called connectionManager which establishes the connection with DB. The dbutil has the openconnection and close connection methods. I have not yet written the insert statements in StudentManager. PLease do Help me

  • Java package and c++ header files

    What is the difference between importing java package and including header files in c++?

    I do not know .. have been away from C/C++ for a long time...
    As much as I remember you can only import one header file each time... also when you import a header file you can just call methods from the header file automatically ...
    However I could be very wrong on this .. really lost touch from C/C++ ...
    regards,
    Sim085

  • Keyword package and import

    helo,
    someone, pls tell me the difference between using package and import? wat are the difference things that they include in our source file?
    in simple terms pls.
    thanks

    http://java.sun.com/docs/books/tutorial/java/interpack/packages.html

  • Package and import

    I have a problem about the syntax package and import . If i create a class file that call "Play.java" and keep it at directory call "test".
    Now , i create the main file that call the "Play" class and this current file keep at directory that call "demo". That is mean the "demo" directory includes the main file and "test" subdirectory. So , what should i do ?
    Thanks.

    I would assume that you have two files and want one of them to belong to a package. If the main directory you are working out of is c:\demo\ (/demo/ on a *nix system) you would have the following in that directory.
    c:\demo\MainProgramClass.java
    c:\demo\test\
    The c:\demo\test subdirectory would then contain a class called Play.java (c:\demo\test\Play.java)
    Right?
    That means the Play.java class belongs to the package 'test', so you would include the following at the start of the Play.java file:
    package test;
    Your MainProgramClass.java file (or whatever you named it) should contain the following import statement:
    import test.*;
    This will import all of the files in the test package (basically everything in the test directory). You could also use 'import test.Play;' if you only want to import the Play class.
    Hope this is what you wanted,
    Ryan

  • Very Basic Package and Import question

    Thanks in advance ...
    - I read/tried all tutorials and documents on the matter
    - the programs are from "Java 2 from scratch" book (Steven Haines)
    - I suspect the problems result from using new SDK 1.4.2 version versus SDK 1.2.1 (that the book uses)
    OK, here we go :
    package Stock1.PortfolioTotalsPanel;
    - first line in PortfolioTotalsPanels.java file
    package Stock1.StockTablePanel;
    - first line in StockTablePanel.java file
    package Stock1.StockTableTabPanel;
    - first line in StockTableTabPanel.java file
    --- then:
    Import Stock1.PortfolioTotalsPanel ;
    Import Stock1.StockTablePanel ;
    NOW : I guess I have to compile 'PortfolioTotalsPanels' and 'StockTablePanel' on their own - which I did successfully and kept the '.class' in the same directory (folder) - Stock1
    When trying to compile 'StockTableTabPanel.java' file...
    StockTableTabPanel.java : cannot resolve symbol
    Symbol : class StockTablePanel
    Location: class Stock1.StockTableTabPanel.StockTableTabPanel
    StockTablePanel stockTablePanel = new StockTablePanel();
    ...and the same error message for 'StockTablePanel' class.
    - I noticed the last line has 'stockTablePanel' instead of 'StockTablePanel' ...not sure if it makes a difference..same with the 'portfolioTotalsPanel'
    I'm out of ideas...HELP !!

    package Stock1.PortfolioTotalsPanel;
    - first line in PortfolioTotalsPanels.java file
    package Stock1.StockTablePanel;
    - first line in StockTablePanel.java file
    package Stock1.StockTableTabPanel;
    - first line in StockTableTabPanel.java file
    --- then:
    Import Stock1.PortfolioTotalsPanel ;
    Import Stock1.StockTablePanel ;
    NOW : I guess I have to compile
    'PortfolioTotalsPanels' and 'StockTablePanel' on their
    own - which I did successfully and kept the '.class'
    in the same directory (folder) - Stock1Both of your source files are in a directory named Stock1, right? Change directory so that you are in the directory above Stock1:
    c:\javastuff\Stock1> cd ..
    c:\javastuff> javac Stock1/StockTablePanel.java Stock1/PortfoloiTotalsPanels.java
    to run them, run them from this root directory:
    c:\javastuff> java Stock1.StockTablePanel
    assuming that that class has a main() method.

  • How to make a package and import it later

    Hi folks,
    this is a very basic question but it is not working to me so i will appreciate your comments.
    I had programmed many classes that are going to be used in many projects so they must be part of a library. Tell me whether or not this procedure is correct ?
    Compile those classes into .class files. Create a package with the jar application putting the .class files into a single file called my_utils ( .jar or .zip ?). Import the package from other projects using the import directive and storing the my_utils file in the project's library directory. Is this correct ?
    Many thanks in advance
    Marla.

    If you want to create a package and then put the package into a jar file, first compile into .class files (in appropriate package directories), then jar all of the .class files.
    Here's a link for a tutorial on packages. http://java.sun.com/docs/books/tutorial/java/interpack/packages.html
    Here's a link for a jar file tutorial. http://java.sun.com/docs/books/tutorial/jar/index.html
    You do not have put the .class files into a jar. Java packages are used for organizing classes, managing class name space, and controlling access to classes. Java jar files are a way to put multiple classes into a single file while compressing the content. Jar files can contain multiple packages and packages do not have to be inside of jar files.

  • To know abt java packages and classes

    please give an idea abt how to find the list of packages and classes
    available in the java packages

    Which ones? If you mean the ones available in the standard Java APIs, check the java-docs:
    1.4.2:
    http://java.sun.com/j2se/1.4.2/docs/api/
    1.5:
    http://java.sun.com/j2se/1.5.0/docs/api/

  • Java packages and dependency

    When java was being developed, what was the philosophy behind allowing packages to be mutually dependent?

    Hello Steve,
    The Acyclic Dependencies Principle tells us that no cycles should exit in a model depicting the dependencies between packages. From what I can gather this is useful when developing large software systems, as each package becomes a viable work unit for an engineer or small group of engineers to control as a series of releases. If the ADP is not complied with, cycles may exist in the dependencies between packages. If a change is made to one of those packages within a cycle, then an ugly situation develops as the repercussions of the change are looped through the cycle back to its source and so on.
    The thing is though, during my readings I came across a statement that said Java packages were mutually dependent. I was just wanting to know why a language that utilises object oriented technology, left the practice of the ADP up to the descretion of the developer? Is the full implementation of ADP on a software system an ideal world scenario?
    Anders.

  • Why jave export and import need to be performed after sys copy Dual stack

    Dear All,
    i have a question why do we have to perform JAVA export in the source and import in the target system
    after system copy ( dual stack ). when whole database is copied. Thank you in advance.

    Hi,
    The reason for this is: because most of the Java configuration lies in the file system and not in the database. This is the reason you need to export Java and then import in system copy.
    Thanks
    Sunny

  • Functionality to handle JAVA Export and Imports in ChaRM covered under SP15

    Hi All,
    Is the highly expected functionality of handling change request management for Java based systems (Export-Import for the Java transports) is covered under the Support pack 15 of SOLMAN4.0?
    If anyone has any information regarding please share.
    Regards,
    Kathir

    Don't believe this is possible without a manual process linked to it.
    The only solution I see for now is that you first only release the TR that need to go to your follow-up system, import them, then perform the manual actions and then release the subsequent TRs, etc...
    Same issue is popping-up when using XI Transports, as each TR has a Change List that needs to be activated. If two TRs within the same maintenance cycle contain the same object, the second TR will fail because the change list for that object is not yet activated. A manual process can solve the issue. At our project, we introduced a new user status for this.
    Best regards,
    Roel

  • Test Packages and import library error.

    Maybe somebody can help me. I'm working with a developer who created our project in NetBeans and uploaded to a CVS. (I am using JSE8) Because he did this it did not generate a "Test Packages" source folder. When I go to my local machine and create a folder named "test" the project creates the correct icon folder.
    1. At this point I select generate test (junit test) of my class in the package com.ca.web.utils
    2. in Test Packages it creates a package named com.ca.web.utils and then the test class classATest.
    3. My imports would not reference the class in the source package it only reference my TestClass in the Test Package.
    for example,
    Src
    com.ca.web.utils
    classA
    test
    com.ca.web.utils
    classATest
    and in my classATest when i attempt to reference classA
    import com.ca.web.utils. (only class in intelisense is classATest)
    Can anybody help?

    Seems if we add the "test" file package to the CVS itself that fixed the problem.

  • Please tell how to use package and import?

    Hi,
    My classes are in different packages
    for eg
    package manage.ui
    package manage.data
    package manage.layer
    if I try to import
    import manage.layer.*;
    in a class in manage.ui
    it shows
    package manage.layer does not exist
    Please help to find the problem?????

    Are your classes placed in a folder stucture that mirrors the package structure? So, like this:
    -manage(folder)
    --ui(subfolder of manage)
    --data(subfolder of manage)
    --layer(subfolder of manage)

  • 2 files in same package and depends each other need import package??

    test1.java and test2.java are in the same package mypackage,
    and test1.java will call methods in test2.java, does it mean
    test1.java needs to import mypackage.test2;
    i.e.
    //test1.java
    package mypackage;
    import mypackage.test2;
    class test1
    }

    no, once classes are in the same packages they are picked up ie tc1 is in tc_package and tc2 is also in tc_package, tc1 need not import tc_package.tc2 to use tc2 methods.
    /Paul

Maybe you are looking for