Hi Punit
If your computer has several drives ("C:", "D:" and so on) and your
current drive is "C:", then:
-- You can use in your program just "/hello"
-- to get the directory "C:\hello" (it works every time).
But if your current drive is "D:", then:
-- "/hello" correspond to "D:\hello".
-- You must explicitly use "C:/hello" in order to see "C:\hello"
If you want to see the "hello" subdirectory of the current directory:
-- Just use in your program "hello" (and not "/hello")
-- or "./hello".
Please try the following code with various values for the string
(instead of "/hello") and see what it prints (it actually prints the
full name of the file as if it existed in the canonical form - you can
see exactly what "/hello" means to your program).
*public class MyClass {*
* public static void main(String[] args) throws IOException{*
* File f = new File("/helo"); // <-- Put here any name you like*
* System.out.println(f.getCanonicalPath());*
* }*
*}*
Hope it helps
Mihai
Le 28/03/2011 10:36, punit jain a écrit :
hey, that code is working if i use that for directory root drives.
i means i supposed to change dirname with "C:/hello", it being work,
bt not working if my directory is in the same folder, where my program
is saved...
On Mon, Mar 28, 2011 at 10:44 AM, Mihai DINCA <mihai.di...@free.fr
<mailto:mihai.di...@free.fr>> wrote:
Hi
First of all, what is the error you get? Is it at compile time? At
execution time? No error but nothing printed?
Then, what is your operating system?
-- If Unix or Linux: is your "hello" directory at the root of your
file system? (I.e. when you type "ls /hello" do you get something
else than an error?).
-- If Wndows: is your "hello" directory at the root of your
current drive? (I.e. if your program executes on "C:", then the
directory "C:\hello" is expected and so on.)
What files exist in your "/hello" directory? If there is no
"xxx.txt", then nothing is shown (not even the message "--> No
"/hello" directory!").
I actually tested the code and it works (just make a copy and
paste from the message). I actually tested the three cases:
1. No "/hello" directory - the message "--> No "/hello"
directory!" is printed ok.
2. The directory exists with no ".txt" file inside - nothing is
printed.
3. Some ".txt" files and some other files in the directory - only
the ".txt" files are shown.
Hope it helps.
Mihai
Le 27/03/2011 23:02, punit jain a écrit :
i have created a hello directory, again i create bt still it's nt
going into the else part.
On Mon, Mar 28, 2011 at 1:19 AM, Mihai DINCA <mihai.di...@free.fr
<mailto:mihai.di...@free.fr>> wrote:
Hi
What I first notice is that there is no "public" class. I
assume that the file that contains all that code is called
"DirListOnly.java". In this case, your class should be "public".
Then, when I execute it, I get a "NullPointerException" as
there is no "/hello" directory, so the array of strings "s"
is null (and s.length issues the NullPointerException as I
call the method of a null instance).
If I create the directory "/hello", then your program works
with no problem. Here is my sugestion:
*import java.io.*;*
*class OnlyExt implements FilenameFilter*
*{*
* String ext;*
* public OnlyExt(String ext)*
* {*
* this.ext="."+ext;*
* }*
* public boolean accept(File dir, String name)*
* {*
* return name.endsWith(ext);*
* }*
*}*
*// class DirListOnly*
*/_public_ /class DirListOnly*
*{*
* public static void main(String args[])*
* {*
* String dirname= "/hello";*
* File f1=new File(dirname);*
* FilenameFilter only=new OnlyExt("txt");*
* String s[]=f1.list(only);*
*_/ if ( s == null )/_*_/
* System.out.println("--> No \"/hello\" directory!");*
/_*_/ else /_for(int i=0;i<s.length;i++)*
* {*
* System.out.println(s[i]);*
* }*
* }*
*}*
Hope it helps
Mihai
Le 26/03/2011 15:28, punit jain a écrit :
Hello can u solve this exception?
import java.io.*;
class OnlyExt implements FilenameFilter
{
String ext;
public OnlyExt(String ext)
{
this.ext="."+ext;
}
public boolean accept(File dir, String name)
{
return name.endsWith(ext);
}
}
class DirListOnly
{
public static void main(String args[])
{
String dirname= "/hello";
File f1=new File(dirname);
FilenameFilter only=new OnlyExt("txt");
String s[]=f1.list(only);
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
}
}
}
On Thu, Mar 24, 2011 at 11:55 AM, Mihai DINCA
<mihai.di...@free.fr <mailto:mihai.di...@free.fr>> wrote:
Hi Amit
I never thought it were a problem in the way you
implemented the classes.
It is just that there is a classical schema for
compiling java classes in packages that it is puzzling
the first time you encounter it.
There are several things to notice:
-- When you compile a Java class, all the referenced
classes must be found by the compiler, either in the
".java" format or in the ".class" format (i.e. compiled
or not).
-- That is why the "javac" compiler needs a classpath too.
-- The current directory (i.e. the one designed by ".")
is by default in the classpath. So if you have several
interdependent classes with no package, you can just:
*cd $MYDIR *(to the directory that contains the
.java classes)
*javac MyFirst.java
javac MySecond.java*
... and so on ...
or (of course):
*cd $MYDIR *(to the directory that contains the
.java classes)
*javac *.java*
-- If a Java class contains a "package" information,
then it is supposed to be compiled and executed in a
directory matching the package name.
For example, if the class "A.java" contains the
declaration "package p" and the class "B.java" contains
the declaration "package p.cls" and the two ".java"
files are in the directory "$MYDIR" then you shoud:
*cd $MYDIR *(to the directory that contains the
.java classes)
*mkdir ./p *(create the directory corresponding to
the package "p")
*mkdir ./p/cls***(create the directory corresponding
to the package "p.cls")
*mv ./A.java ./p/A.java***(put the class in the
proper directory)
*mv ./B.java ./p/cls/B.java***(... same thing ...)
*javac p/A.java***(compile A)
*javac p/cls/B.java***(compile B)
*java p.cls.B***(execute main in "./p/cls/B.class")
Of course:
-- A must call the methods in "p.cls.B" (or have an
"import p.cls.B" or an "import p.cls.*") and not
directly in "B"
-- and B must call the methods in "p.A" (or ...) and
not directly in "A".
Hope it helps
Mihai
Le 23/03/2011 10:27, Amit a écrit :
Hello Mihai,
Thanks a lot for replying to the post.
I finally worked around it. There was no problem in the class and
the
way it was implemented. Everything was clearly instantiated and
referenced.
The main culprit was how I was compiling it. I was able to compile
the
Food class, but not FoodMain class as it was looking for Food
class. I
was able to compile and generate the .class file, by compiling both
the files together bu using "javac *.java".
But again after compiling I was having trouble running the .class
file
- its gave ClassDefNotFoundError, which I guess was because of the
classpath problem. As I was compiling it from the subdirectory where
it resided.I managed to run it flawlessly by going to the root
folder
(Linux) and then running with the classpath option, as here : "java
-
classpath<folder-where-package-is-present> <package-structure>".
Thanks again for replying to my post, and I gave the explanation so
as
it can help other people having such problem.
With regards
Amit.
On Mar 23, 12:39 am, Mihai DINCA<mihai.di...@free.fr>
<mailto:mihai.di...@free.fr> wrote:
Hi Amit,
There must be some mismatch in the way the class Food.java is used
inside FoodMain and where the files are physically located. Can you
send
the code of FoodMain and explain in which directory are located the
files?
I mean, in the DOS window in which you type "javac FoodMain.java",
what
is the current directory and in which sub-directory are located your
.java files?
Hope it helps
Mihai
Le 22/03/2011 16:47, Amit a crit :
I dont understnad what the problem is.
In order to check my logic, I recreated the same scenario in
Netbeans
and it works perfectly. Can anyone please suggest something?
I finally managed to compile my FoodMain.java by compiling both the
files simulataniously with "javac *.java", but when I try and run
FoodMain - its gives me "NoClassDefFoundError". What do you think
the
problem is?
Thanks.
On Mar 22, 5:27 pm, Amit<amitah...@gmail.com>
<mailto:amitah...@gmail.com> wrote:
Hello,
I am doing the following homework exercises which asks us to create
two .java files in package foodpackage.fruitpackage. The files are
Food.java and FoodMain.java.
I created and complied Food.java and when I am doing the same for
FoodMain.java and try to access Food class from it says symbol not
found.
I am pretty sure that I have done everything correctly. And in any
case they are in the same package, so Food class should be available
to FoodMain class. Both the files are in fruitpackage directory,
then
why doesnt it work? Can anyone help me on it?
Thanks
With regards.
--
To post to this group, send email to
javaprogrammingwithpassion@googlegroups.com
<mailto:javaprogrammingwithpassion@googlegroups.com>
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
<mailto:javaprogrammingwithpassion%2bunsubscr...@googlegroups.com>
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
Punit
--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en