Setting Up Java 0.o

Started by
1 comment, last by gimbal_ 15 years, 9 months ago
Edit-------------------------------- Managed to Fix it, for any body who is interested i used java -classpath . myFirstApp to set the class path to the local directory, still clueless about what a class path is tho, so if you can answer that question =) thanks. ------------------------------------ Man, looked all over the net and I couldn't find anything about this error I keep getting. I managed to set my paths and I can compile my code into a .class file but if I try to run it with the java command I get this: Link to Pic: http://img224.imageshack.us/my.php?image=jaercu0.jpg The source of my file is: Java

 class myFirstApp
{  
        public static void main(String[]args)
        {
           for(int i=0; i<100; i++)
           {
           	   System.out.println("Hi");
           }
        }
}

 

Yes i realize the for loop is overkill but I don't know how to get input with java so it might terminate super quick. Can anybody help?
Advertisement
java myFirstapp

not

java myFirstapp.class
Quote:
still clueless about what a class path is tho, so if you can answer that question =) thanks.


the classpath is much like the PATH of the OS; only it is a path for java classes. You can setup a classpath (for development purposes) by adding a environmental variable called CLASSPATH. For example, lets say you use windows and you have a directory called c:\projects in which you store all your java projects. Your classpath would be this:

.;c:\projects

The '.' in the classpath means that the 'current working directory' is always part of the classpath. The CWD is always the directory where you invoke the 'java' command. The ';' in the classpath is the default separator character of the OS. On windows it is ';', on Linux it is ':', so if you are on Linux the classpath might like like this:

.:/usr/you/projects

Okay. I hope you are familiar with packages, if not go figure out what they are first. 'java.lang' is a package, 'java.util' is one, etc. You can put your application classes in their own package and it is actually the correct way of doing things; having classes without a package is not recommended and severely limits your choices.

So lets define an application. I will call it 'texteditor. I live in the netherlands and my company is called foo enterprises, so the package I choose for my application is 'nl.foo.texteditor'. To make this work, I *have* to store my classes in

c:\projects\nl\foo\texteditor

Why? because the c:\projects directory is in my classpath and my package is nl.foo.texteditor. See how it works? The package matches the directory structure INSIDE my projects directory. This has to match 100% case sensitive. The second thing I have to do is that I have to add the following line to the very top of all classes in the nl.foo.texteditor package:

package nl.foo.texteditor;

Again case sensitive. If you make a typo java will give you an error about the class being in the wrong package.

Of course we are free to add more subpackages to the application. If I want to have classes in nl.foo.texteditor.gui then I add them to the directory 'c:\projects\nl\foo\texteditor\gui', it is completely up to you.

Okay, another fictional fact about my application is that the class with the main is nl.foo.texteditor.TextEditor. To invoke this class I can do this:

java nl.foo.texteditor.TextEditor

Because of the CLASSPATH environmental variable the JVM can find my class by looking at the package. Where you invoke the java command doesn't matter.

When it is time to make your application available to other people, lookup what an executable jar is ;)

This topic is closed to new replies.

Advertisement