Results 1 to 6 of 6
Thread: can't import my own class
- 10-13-2009, 02:46 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
can't import my own class
Hi,
I am a complete noob when it comes to Java and so I hope someone can help me here. I am not new to programming. Have used C++, Python, Ruby (somewhat), Delphi, etc. However, I feel like I am having to make a bit of paradigm shift when it comes to Java. I am using Java on Windows XP basically from the command line without an IDE. (I don't think I've got enough RAM to run an IDE at the moment, only around 480MB).
I am basically trying to find out how to 'import' my own classes. None of my random guesses is bearing any fruit. I am imaging that
import packagename;
should do the trick but I keep getting 'bad class file' error when trying to do the import. Someone told me that I have to use the right directory structure but that I am confused about. I am confused about whether case matters on windows directory names or only file names?? I understand that package names are lowercase by convention. Do I have to pass anything special on the javac compiler command line to import the extra class?
It seems that every irc channel that I come across hates java newbies so your help would be greatly appreciated.
Regards
Glenn.
- 10-13-2009, 03:22 AM #2
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
So, you want to import your own classes from one project to another (or copy to another project to leave no changes in that project).
1. Look for your projects - they should look like folders
2. Click into one of them where you want your class
3. The class has the file name of ".java"; select it but don't open it
4. Press ctrl+c or right click and select "Copy"
5. Go back and click into the other project where you want your class to be imported
6. Press ctrl+v or right click in and empty space of the folder and select "Paste"
Open your project; it should be there, I hope
-
Copying and pasting files will work but is not a robust long-term solution. The best solution is to be sure that the packages of the class files that you're trying to use are on the current classpath. Take a look at the Java tutorials and also search here and the forums.sun.com regarding classpath for much more information on this.
- 10-13-2009, 03:50 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
Ok, my apologies if I've been a little unclear. I've prepared a minimal test case to illustrate my problem.
In App1.java
In Class1.javaJava Code:import class1.*; public class App1 { public static void main(String[] args) { } }
Both of these files are in the same directory. Do they need to be in different directories? If so, what should the directories be called and is the spelling of the directory case sensitive?Java Code:package class1; public class Class1 { int a = 23; }
Class1.java compiles ok. App1.java does not. I am wondering if its either a case sensitivity problem or directory layout problem. However the java tutorial at java.sun.com seemed to suggest that it wasn't.
This is what happens when I try to compile App1.java
There are just so many things I could be doing wrong and I don't know where to start. Anyway I hope this example helps clarify my problem.Java Code:C:\Projects\Java\Minimal>javac App1.java App1.java:1: package class1 does not exist import class1.*; ^ 1 error
- 10-13-2009, 04:25 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, create a folder name 'class1' in your project folder and add your class Class1.java in that folder.
- 10-13-2009, 04:53 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Eclipse 3.5<import *.jar -Need help>
By roughjj in forum EclipseReplies: 1Last Post: 09-09-2009, 09:24 AM -
How could i import DLL in NetBeans?
By makpandian in forum NetBeansReplies: 0Last Post: 02-13-2009, 12:53 PM -
I am using Jdk1.3. I am not able to import Scanneer class in my program.
By tabrez_k81 in forum New To JavaReplies: 4Last Post: 12-29-2008, 02:20 PM -
Import not working
By ribbs2521 in forum New To JavaReplies: 5Last Post: 12-16-2008, 06:14 AM -
import statement.
By diRisig in forum New To JavaReplies: 2Last Post: 02-08-2008, 12:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks