Java file layout without IDE (OS: Ubuntu) -


i writing java code without ide, got little problem when try run code after compiling it. (i using ubuntu 64)

$ javac classname.java $ java classname not find or load main class classname 

my directory structure following:

projectname
----- packagename
---------- classname.java
---------- classname.class

my code start writing down packagenmae. when remove package statement, works. while error occur when statement included.

package packagename;  public class myclass {     // .... used in main class }  public class classname {     public static void main(string args[]) {         // ....     } } 

could tell me problem.

the main issue trying run command from. not run inside package directory, root of package tree (in example, projectname directory).

from there, should doing:

javac packagename.classname

which tells compile "classname"[sic] inside package "packagename"[sic]. way doing it, telling compile class not part of package (which discouraged).

notes:

  • each file can have "general" class, name of file.

you may define inner classes inside class, inside code block of class.

package packagename;

public class classname { public class innerclass { ... }

public static void main(string args[]) { ... } }

  • file name , class name must same. includes case (lowercase or uppercase) of name.

  • class names begin in uppercase.

  • package names should in camelcase.

  • usually not want leave compiled (.class) files source (.java) files. usual structure is, @ minimum:

--> project  --> src --> mypackage --> myclass.java              --> bin --> mypackage --> myclass.class 

so need copy .class files distribute executable.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -