segunda-feira, 22 de março de 2010

How to compile a program?



After installing the JDK, just use the program "javac" to compile your files. Java. The mean javac "Java Compiler". For example, say you have created a file called PrimeroTeste.java.






To compile it, type:










javac PrimeiroTeste.java






The javac should be in your path. He is in the installation directory of your java in the / bin.










How do I run a program in Java?










Excecute it with the command "java" followed by the name of the file without extension. For example, if you compiled a program called PrimeiroTeste.java, you see that was created a file called PrimeroTeste.class, which is the binary code of your program. To run it, simply type










java PrimeiroTeste






Note that only reported the name of Progarm, without any extension. This will cause the JVM, Java Virtual Machine is started, and "charge" your class by running it.






When trying to run any program the following error occurs:






Exception in thread "main": Java.lang.NotClassDefFoundError






You're typing the name of your class correctly? Remember that java is case sensitive, ie it is case sensitive!






This error can also occur when the CLASSPATH variable is not properly configured. The most common mistake is forgetting to add the current directory (specified by a point) to the CLASSPATH. Sometimes this variable is set as follows:










CLASSPATH = / home / user / java / algum_pacote.jar






Note that in this case only that the CLASSPATH pointing to the file "algum_pacote.jar", not containing the "point". The correct in this case is










CLASSPATH =.: / home / user / java / algum_pacote.jar






Note: The same goes for Windows, with the difference that the directories are separated by a semicolon instead of colon.






Also make sure you are in the right place! You should be in the directory where the class is, or if it is a class within a package (there is a package statement), you should be in the "root" of the package, and call the java with the full name class.






When trying to run a program, it does the following error:






Exception in thread "main" java.lang.NoClassDefFoundError: myprogram / class






Very close to the previous problem, this is actually the lack of attention to trying to run the program. As explained above, we run the programs made in Java without using the file extension. In the example above, the User tried to run the program as follows:










java MeuPrograma.class






Thus, the Java program tries to find the "class" within a directory (package) called "myprogram", which of course does not exist. To resolve, just run like this:










java myprogram






When running the program, gives the following error:






java.lang.NoSuchMethodError: main






One of the classic mistakes of the gang that starting in Java. This error occurs when a program that does not contain a valid declaration of main () function is executed. Consider the following examples:










1 JavaMainBug1.java2 public class JavaMainBug13 (4 public void hello () 5 (6 System.out.println ( "Hello");) 7) 8






Compile (javac JavaMainBug1.java) and try running (java JavaMainBug1).






Note that the error (java.lang.NoSuchMethodError: main) occurs. This is because it has not been declared the function main ().






To solve the problem, simply add the following:










1 public static void main (String args []) 2 (3)










The function main () must necessarily be "void", and contain an array of parameter,


type String. See this another example:










1 JavaMainBug2.java2 public class JavaMainBug23 (4 public static void main () 5 (6 System.out.println ( "Hello");) 7) 8






Compile it and try to run. The problem here is the lack of the parameter to the function. A final problem that is likely to occur is to define the main () function like this:










1 public static void main (String args)






Note that the variable "args" in this case is not an array, when in fact it should be.






Other examples of signature of main method that are not as usual, but they work:










1 public static void main (String args []) 2 static void main (String [] args) 3 final public synchronized void main (String arguments []) 4 ...






Do not worry if you do not understand these lines of reporting the main, practicing java you understand them.






How do I see which version of Java I'm running?






Use the command:










java-version






I created a file called PrimeiroTeste.java, but the compiler refuses to compile it, and because I have to use the extension. "Java" in the source code files?






The javac compiler assumes that any file with Java source code should have the extension. Java. Anything short of that do not reach the initial process of compilation.

Nenhum comentário:

Postar um comentário