Importing my own classes

I have 2 simple classes in the same directory and I'd like to test the use of import to import one class into the other. Upon compilation of the class thats doing the import, I get a compilation error. Herewith the details, where could I be getting it wrong.
****code for class to be imported****
import java.util.Date;
public class printer
public void print()
System.out.println(new Date());
System.out.println("Hello World");
--The above code compiles with no problems.
***Code for the main class that imports the above*****
import printer;
public class myapp
public static void main(String[] args)
(new printer()).print();
-upon compiling the second class above, myapp.java, I get the following errors:
C:\java\source>javac myapp.java
myapp.java:1: '.' expected
import printer;
^
myapp.java:1: ';' expected
import printer;
^
2 errors

Moreover, classes do not need to import any class which is in the same package as their own, and since your printer class is in the default package (if you call that a package) you shouldn't need to import it into myapp anyway

Similar Messages

  • HELP: Import my own class in JSP ???

    Hi, all!
    I read many previous topics in this forum but no one works for me!
    Please someone help me!
    I'm using Tomcat 4.1.12 and my JSP scripts work just fine, but I need to import my own .class file. I just don't know where to put it so Tomcat can find it!?
    I have this environment variable:
    JAVA_HOME=.;c:\jdk1.3.1
    And in my code I'want to place something like this:
    <%@ page import="myClass" %>
    But I get a "can not resolve symbol" exception.
    Thanks for readind this, and please help if you can!
    Bye.
    adriano

    Within the Tomcat directory, under your application directory place the class file/s into the WEB-INF/classes directory. If your classes are part of a package place the entire directory structure of the package under the WEB-INF/classes directory.
    The import in the JSP is the same as for any Java class.
    Hope this solves your problem.

  • Problem to Import my own classes

    I try to import own classes to unother class
    I have all classes in the same map
    import StockTablePanel;
    when I tried to compile the code it says
    �.� expected
    StockTablePanel;
    What is wrong?

    I try to import own classes to unother class
    I have all classes in the same mapAlso if by the same map you mean the same folder then you don't even have to import the class.

  • Help! Cant import my own classes?

    Hi all, I really need some help.
    I created a swing browser application in my working folder called Swiki.
    Then I found a open source package that I want to use some of its classes.
    I put that package into a subfolder (cos it has many subfolders itself).
    I am having a problem importing that package into my own browser.java.
    I tried:
    import Kizna.*;
    OR
    import Kizna.src.com.kizna.*;
    when I try to create an instance of one of the classes:
    public HTMLParser myParser
    The error says:
    "HTMLParser.java": Error #: 901 : package com.kizna.html stated in source C:\Swiki\Kizna\src\com\kizna\html\HTMLParser.java does not match directory Kizna.src.com.kizna.html at line 1, column 19
    can someone help?
    thanks
    TC

    When the compiler and runtime are trying to find files, it converts the import statements to directories, so:import com.kizna.html.*;will look under a directory structure "com/kizna/html/". Then, it will look through your classpath and look to see if this directory structure exists in any of the directories specified in the classpath. That means, if your classpath is ".;C:\SomeDir;C:\AnotherDir", it will first look for class files in ".\com\kizna\html\". If it doesn't find the file it is looking for in there, it will look in "C:\SomeDir\com\kizna\html\". If it still doesn't find the file, it looks in "C:\AnotherDir\com\kizna\html\". If the file doesn't exist in any of these, you will get a compiler error.
    Points to remember:
    1)     Classes in a package, when compiled, must always be in a directory tree that matches the package structure. That is, if you have a class called MyClass in package mypackage.subpackage, then the "MyClass.class" file must be in a directory called "subpackage" which in turn is in a directory "mypackage".
    2)     Your import statements must match the actual package name of the class. You must import "mypackage.subpackage.*" above; importing "subpackage.*" will not work.
    3)     Your classpath must consist of all the base directories of your package directory tree. In the above example, your classpath must include the directory that contains the directory "mypackage".
    Thus, the compiler will look through all combinations of <classpaths>/<import package tree>/MyClass.class to find the file for class MyClass. Here, <classpaths> are the paths given in your classpath and <import package tree> are thre directory structures for the packages you import. So, in your case, you want your classpath to include "C:\kizna\classes" and your import statement to be "import com.kizna.html.*". Note that including "C:\kizna\classes\com" in the classpath and importing "kizna.html.*" will not work (even though the combination gives the correct "location") because your import statement does not match the actual package name given to HTMLParser. While this may seem strange that this isn't allowed, it is done to ensure that classes are unique (classes may use the same name if they are in different packages).

  • How to import  my own class in jsp file

    I have a jsp file in jsp\ directory and a class in web-inf\classes\ directory. How do I import the class in jsp so i can use it in my jsp file. Thanks,

    This is your lucky day, wennie.
    The correct forum to post this is:
    [http://forums.sun.com/forum.jspa?forumID=45]

  • NullPointerException - importing my own class to JSP

    Hi.
    I'm writing a JSP page. I also made a class in WEB-INF/classes/org/MyXml.class. When I try to run this class from console, there is no problems at all. But if i try to use function from that class in JSP, i get this (the same error i get whichever function i try to use (btw, all functions are static, as they should be, right?)):
    java.lang.NullPointerException
         org.MyXml.CountProducts(MyXml.java:46)
         org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    I used this class like this:
    <%@ page import="org.MyXml" %>
    <%
    MyXml.CountProducts();
    %>
    I'm also a bit confused, it says that error is on line 46. Well, there's just a comment!
    Please help. Thanks.

    pqeuens,
    MyXml is his class. his methods are static, so, import="org.MyXml.*" won't work. It would either be import="org.*" or import="org.MyXml" (which he has)
    paull1911
    "What file are you looking at to find line 46? It is the java file (a servlet) corresponding to the jsp file."
    It should actually be in the org.MyXml.java file. The stack trace line is:
    java.lang.NullPointerException
    org.MyXml.CountProducts(MyXml.java:46)
    someone_
    You know from the above error line that the error is in MyXml.CountProducts method. After making sure you are working with the most up-to-date version of the class, (if that doesn't help you get a more accurate line number) walk through the CountProducts method and see where you might be trying to access an object that has not yet been instantiated (either using someObject.someMethodOrMember, or someArray[index])

  • Import my own class into servlet problem

    Hello, everyone.. I fail to import java classes that i create myself into the servlet. I wonder what is the problem. I would feel grateful if anyone can solve my problem. I noe that if it is in the package let say myclass , C:\jakarta-tomcat-4.1.29\webapps\lis\WEB-INF\classes\myclass. It will be
    package myclass;
    import myclass.*;
    But, I put all my class file into below directory not in a package.
    C:\jakarta-tomcat-4.1.29\webapps\lis\WEB-INF\classes
    and i wish to include let say DBManager.class file into Login.class file which in in the same directory as above. What is the correct code i should put? Thanks for anyone who willing to help me out ~

    You don't need to import classes from the same package. They are automatically available in the same namespace. Just use them.
    package myclasses;
    import myclasses.*;  // this line does nothing
    import myctherclasses.*;  // this line is necessary to use classes from another package.Having said that though, it is always preferable to put your classes in a package. Classes in the "unnamed" package are not guarunteed to be available anymore.
    Particularly servlet classes which are not in a package aren't found by Tomcat.
    In short: always put your classes into packages.
    Cheers,
    evnafets

  • Import my own class

    I created two classes in the same package (myclass). one of them is using another class. what is the syntext of import another class: I tried import myclass.* also import myclass.class1. it didn't work. Thanks,
    j

    For class1.java in mypackage directory:
    package mypackage;
    public class class1 { }
    For class2.java in mypackage directory:
    package mypackage;
    public class class2 { }
    For a file myclass one level out of mypackage:
    import mypackage.*;
    public class myclass {
    public static void main(String[] args) {
    class1 c1 = new class1();
    Something like that should work.

  • Can't import own classes...

    I'm very new to java. I rented a book called Java 2 Grand Cru to learn it on my own. It is based on Java 1.3 but I have 1.4 installed. Now I wanted to write a simple program with a class in an other directory. I added that directory to CLASSPATH so my compiler could find it. These are the 2 programs
    import printer;
    public class test
         public static void main(String[] args)
              (new printer()).print();
    }and
    public class printer
         public void print()
              System.out.println("test");
    }So I want the first class to use the second to print "test"!
    Now I can compile public class printer without any problems but when I compile the first one, I get this:
    test.java:1: '.' expected
    import Printer;
                   ^
    1 error
    Process Exit Code: 1
    Time Taken: 00:00
    i don't get it. Is there anything changed with the import instruction in the new version of Java2?
    regards

    It is the error of my code but probably I did something bad with the copy paste thing :-)... The 'P' should be a 'p'
    Anyhow, I don't understand your first comment. 'printer.class' isn't a default package. I made it myself...
    regards

  • Problem with creating my own class...

    Hi all,
    Purpose with this program:
    I want to create my own class StringThing which take a string parameter and transform the content toUpperCase().
    I have created a simple class StringThing which look like this:
    class StringThing {
    public String upperize (String u) {
    u.toUpperCase();
    return u;
    }And my class where I use the StringThing looks like this:
    import java.io.*;
    class UseStringThing{
    public static void main (String arg[]) throws Exception {
    BufferedReader keyboard = new BufferedReader(
    new InputStreamReader(System.in));
    String s,p;
    System.out.prinln("write a sentence!");
    s=keyboard.readLine();
    StringThing thing = new StringThing();
    p=thing.upperize(s);
    System.out.println(p);
    }Am I not supposed to transform my string parameter in my class by using for instance toUpperCase()? Or is there some fundamental rule or piece of code which I forgot?
    Thanks in advance,
    /Beginner-T-who-ripps-his-hear-over-this-small-problem

    Strings cannot be modified. What this line does:
    u.toUpperCase();
    is to create a new String object. toUpperCase() returns the newly created string and you are ignoring the returned value. What you want to do is:
    return u.toUpperCase();
    or
    u = u.toUpperCase();

  • Import my own package??!!!

    hello guys,
    I'm having a litle difficulties importing my own package.
    here is my example:
    I have a folder on C:\Users\User\Desktop\test2 in wich u can find a file called "hello.java" and another folder called "hi" which contains a file "hello2.java"
    in the file hello.java i want to create an instance of "hello2" so it is clear that i have to import from hello.java the package containing hello.class
    SO WHAT I DID IS THE FOLLOWING:
    first in enviroment variable i added to my CLASSPATH the path C:\Users\User\Desktop and in the file " hello.java" i wrote at the top:Import test2.hi.hello2
    it is still not working.
    i read somewhere that my package should have the following form: fr.inria.itey which i did not understand.
    can u plz give from my example what is my package name? and finally how to solve the issue
    thank you in advance for your help

    Hi ralfph.
    I suggest using an IDE such as eclipse. It has an auto complete utility that allows autogeneration of import code which is guaranteed to be correct.
    Based on your post, I'm not sure if it is a typo but you mentioned you wrote at the top "Import test2.hi.hello2".
    I would like to know if you got a compile error as "Import" should be typed as import with a small i.

  • How to get imports of a class OR how to fix javadoc-@link/@see

    I'm generating classes based on existing classes. The new code also contains the javadoc-comments. These comments can contain @link- and @see-tags.
    In the original source everything works nicely since all necessary classes are imported. However, inside the generated class I currently don't have those imports available to me (I'd assume that TypeElement would provide a method to get them, but it doesn't). One way would be to run through all comments and replace all class names with the fully qualified name. However, that's one time consuming process and I know that at one point people will forget to always write the fully qualified name.
    The main goal is to have javadoc generate the documentation with all cross-linking working.
    Is there a way to make @link- and @see-tags work?

    Darryl Burke wrote:
    I don't think what you're looking for is possible, and I don't think it can be possible. Imports disambiguate classes.
    Without imports, how can any system possibly differentiate between java.util.List and java.awt.List? or the three Timers in the JDK? or indeed any of your own classes that might share the same name.
    dbThat's why I'm looking for a way to get those imports. If I access any type in the annotation processor I always get the fully qualified name. Since in a way I am the annotation processor, I'd assume that I have access to how the compiler figured out which List the original source refers to.
    I mean if the code is
    java.util.List<String> myList;it's easy. However, I usually code
    import java.util.List;
    List<String> myList;and the compiler does the match. If the processor accesses the type of myList, it sees java.util.List in both variations. Obviously the compiler did some magic there. In my naive thinking I'd assume that I get access to the magic. The import declaration at the top of the source would be the "magic" to me.
    The tragedy starts if the original code looks like this:
    import java.util.List;
    List<String> myList;
    java.awt.List myGorgeousList;
    /** blah blah {@link List} blah
    */This is the reason why I can't collect all kinds of types I run into while parsing the original source and simply pick the first matching one. I'd end up with both java.util.List and java.awt.List. Now how would I know which List the comment is referring to? I can only do this if I have access to imports. Then I'd see that there's a List declared as java.util.List. Since the List in the comment isn't fully qualified, java.util.List would be my choice.
    But I don't have this! I already checked the API of Java SE 6, whether there is some getter-method with "import" in its name, but I couldn't find anything. :(
    If I code something in an ambiguous fashion, the compiler complains and I can fix this. However, with my current knowledge I can't think of a way to solve the ambiguity inside my processor.
    Which is why I started this thread :)

  • Importing vs. extending classes

    ok, thanks for replying, ok then, what is the difference between importing a class and extending a class, i know that when you extend you add all the properties/methods of that class to your own class, but im still un sure of what this actually does for you and your program, im pretty confused on the topic and i couldnt find the difference between the two on the internet, also how do you know which one of the two you need to do? thanks, any help is appreciated

    This is what i understand so for (i got this from my book) :
    Inheritance is the is the system of extending a class to create a new class. All the classes so far have been created by extending the MovieClip class.
    When you see the keyword 'extends' in the class definition, you know that inheritance is at work:
    public class NewClass extends MovieClip
    This allows the new class to 'inherit' all the properties and methods of the class it extends. The new class can then use all those properties and methods in addition to any new properties and methods that the new class defines.
    ok, so i understand extending classes, but its still the importing part which still confuses me, i am a VERY beginner, so if you could explain it in very plain terms i'd really appreciate it, thanks!

  • Can I get a weblogic Initial context without importing all weblogic classes ?

    can I get a weblogic Initial context (with weblogic.jndi.WLInitialContextFactory)
    without importing all weblogic classes ?

    I ran my client through all its functions.
    I then took the access.log file and parsed out a list of all the class
    files that were downloaded and built a script to create my
    weblogiclient.jar file.
    Before running the client we had to:
    With WL5.1 I think we had to unjar the weblogicaux.jar file into the
    serverclasses directory so the client could load them all individually.
    Make sure you clean up after you are done with this.
    With WL 6 we did not have to do that.
    The access.log file is the key to building your own client jar file.
    We also require the use of the Java 1.3 plug-in by our clients (for
    Applets) so we can do multiple Jar caching.
    Tom
    Dominique Jean-Prost wrote:
    Hello tom
    What do you mean by "use the Weblogic class loader" ?
    Could you explain whath you exactly did to find all the classes you need ?
    regards.
    dom
    <nospam@nospam> a écrit dans le message news: 3ABF3EC2.9010200@nospam...
    You need the classes one way or another. BUT you do not have to
    redistribute the whole Weblogic.jar file. Use the Weblogic class
    loader then run your program. The weblogic access.log should have a
    listing of all the classes you need to use and you can build your own
    sub-jar. My experience is that this new jar is significantly smaller.
    Ours is around 600K instead of 15MB.
    Tom
    Dimitri Rakitine wrote:
    Yes, if, for example, you can network classload from WebLogic. Talking
    to a WebLogic
    server means WebLogic RMI (unless you use RMI/IIOP in which case youdont need any
    WebLogic classes) which needs WebLogic-specific classes, so you clientapplication needs
    to get them from somewhere - local classpath or remote WebLogic server.
    David Dahan <[email protected]> wrote:
    I mean without a classpath to all weblogic classes.
    can I get a weblogic Initial context (with
    weblogic.jndi.WLInitialContextFactory)
    without importing all weblogic classes ?

  • How can I create a new own class in WebDynpro?

    I test the Web Dynpro for a while and found all the java classes were generated automatically. I've tried to create an own class. But after I rebuild the project, all of my own classes were cleared.

    Hi
    You can create your own class as mentioned in above replies or one more way,
    1. first you create your java project seperately,  there you can write all your business needs and then, make a jar or that then, you can include that project into your WDproject by
    rightclick on WDproject>properties>libraries>addexternaljars>ok
    2. create seperate java project
    rightclick on WDproject>properties>Projects>selectYourjavaProject>ok
    Regards
    Abhijith YS

Maybe you are looking for

  • How to display field value only once in REUSE_ALV_GRID_DISPLAY

    hi experts,                i am using REUSE_ALV_GRID_DISPLAY, for alv outpur display.but i want one of the field in output ,not to display the value which is of same, it have to be displayed only once, I mean i have a number which contains multiple l

  • Macbook Pro freezes, shuts off for 10 seconds, and then returns to normal

    My 13.3" Macbook Pro has this issue of shutting off for 10 seconds and then turns back on to normal. Usually it happens whenever I wake the laptop with the lid open, whenever I open the lid "too fast" from clam shell sleep, and whenever I reopen the

  • Can't connect to Configuration Manager Service manager

    So after a failed upgrade to R2, I've had to restore the database as the R2 upgrade corrupted the DB. I am now working though the minefield of component errors. However I can't connect to the configuration manager service manager, I get this error: E

  • SC Item in transfer process

    Dear Gurus, Issue:      SC approved while IPC server was down.      PO not generated.      SC stacked in the status u201CI1111u201D-Item in transfer process-      Alert message appears under LOCAL error Actions taken:      Not able to delete via

  • Dates to compare in query from ODS

    Hello, I am just starting to use Bex Query Designer and don't know how to compare 2 dates. Example is ODS from PM-ORDERS area. For one order I have to compare 2 dates and count the total of orders if date 1 < date 2. These 2 dates are in the record P