Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem with class import

  Asked By: Nisha    Date: Dec 26    Category: Java    Views: 754
  

I'm new to java and I'm having a problem with classes.

I cleate two files i.e. Class1.java and Class2.java

Class2 imports Class1 (that's in the same directory)

so in Class 2 I have some code like this:
====================
import Class1;
====================

I can compile without problems Class1 but when I try to comple
Class2 I have this error:

Class2.java:1: package Class1 does not exist
import Class1.*;
^

Please note that my classpath variable is set to "." and nothing
changed even when I tried to compile specifying the -classpath
parameters.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Adalwin Fischer     Answered On: Dec 26

try import  Class1; - without the .*

 
Answer #2    Answered By: Skye Hughes     Answered On: Dec 26

the problem  is when u say

import class.*;

it means that u are specifying class1 as one of ur package  ie (folder class1
containing all ur project classes)but class1 is one of ur class  not package.Thus
the problem

remedy either create a package (and folder in windows )place all ur classes  in
that directory.

 
Answer #3    Answered By: Funsani Chalthoum     Answered On: Dec 26

I think the following solution might help you.
Since class1 and class2 are classes,don't specify any import
statements as only packages can be imported.but before compiling
class2 make sure you have set  the classpath  to class1.


suppose if u have arranged your file as follows :
c:\Demo\class1.java
&
c:\Trial\class2.java

then before compiling class2.java set the classpath as
set classpath=.;c:\demo

 
Answer #4    Answered By: Randall Franklin     Answered On: Dec 26

I recommend putting *all* your java  source files  in one single tree.
Then only compile  the main java file. Sometimes you need to compile
more java files.

src (dir, c:\programming\src for instance)
+- com (dir)
...+- yourcompanyname (dir)
......+- app (dir)
........+- test (dir)
...........+- SomeTest.java (file)
......+- util (dir)
........+- Util.java (file)

SomeTest.java file:

package com.yourcompanyname.app.test;

import java.io.*;
import com.yourcompanyname.util.Util;

public class  SomeTest
{
public static void main(String[] args)
{
Util util = new Util();
InputStream is = new FileInputStream(".");
}
}

Compile with:
javac -sourcepath pathtosrc com/yourcompanyname/app/test/SomeTest.java

run:
java com.yourcompanyname.app.test.SomeTest

And to former C and C++'ers, do not write Makefile's with .java to
.class translation rules. A java file translates to one *OR MORE*
class files. In Java you don't have a build process figure out how to
generate a class file, but you simple throw all your source to the
compiler all at once.

If you ever get fed up with the compilation speed of javac, try IBM's
jikes. It's a replacement command line java compiler that's
rediculously fast. You can write java source files till the cows come
home, but they will always compile in a couple of seconds.

 
Didn't find what you were looking for? Find more on problem with class import Or get search suggestion and latest updates.




Tagged: