4

Hello so I'm trying to Import a class from another project I made but I cant get it to work.. this is my code:

import program.GUI;

public class name {
//code
}

I get the following error

Opens the new class wizard to create the type.

package: program
public class GUI {
}

I have created a prog02>src>program>GUI.java

How can I solve it.

Premo
  • 61
  • 1
  • 8
  • Possible duplicate of [Import a custom class in Java](http://stackoverflow.com/questions/7869006/import-a-custom-class-in-java) – wadda_wadda Oct 27 '15 at 22:56
  • What is the path for name? – Yassin Hajaj Oct 27 '15 at 22:56
  • You don't need the colon after your package declaration, but you *do* need a semicolon after you're done declaring it. Example: `package MyCoolPackage;` – wadda_wadda Oct 27 '15 at 22:57
  • I have two projects and trying to import one class from the first project to the second one. path for the class I'm trying to import is prog02>src>program>GUI.java program is the name of package – Premo Oct 27 '15 at 23:01
  • In this case, you must add the second project to your `.classpath`. – wadda_wadda Oct 27 '15 at 23:06
  • what I did is I went to the name of the project right clicked >build path> configure path>projects and added prog02 is this correct?? – Premo Oct 27 '15 at 23:14

2 Answers2

1

I think I see the problem here...

I don't know if your running java from cmd, but my explanation is going to assume you are. Outside of using IDEs like Netbeans and Eclipse that pretty much streamline the build process of java code, it's good to know how it works.

Ok, here goes:

1) Compile your java source code files into bytecode (.class files) by invoking JAVAC

javac [optional flags] [path to file intended for compilation]

This creates the bytecode that the JVM will need in order to execute.

2) Invoke the Java interpreter (JVM) to run your bytecode.

java [optional flags] [name of class file w/o .class extension]

If everything goes right this command will create a JVM process with main.java being the main entry to the program that creates the initial thread that runs your program.

Here what you should write to get you program to compile with you package dependencies.

  • cd to base dir that contains program with main method (src)
  • javac -cp . program/ (check by going to dir to see if a GUI.class file was generated)
  • javac -cp . Main
  • java -cp . Main

That should do it. Otherwise from and IDE standpoint you either don't have file in the right directory or you are not using the right syntax for specifying your package and import.

coderrick
  • 1,011
  • 1
  • 8
  • 25
1

The package declaration on the GUI class is not right. It should be:

package program;

public class GUI {
}

You may also find this useful: Java Code Conventions

Juan Bustamante
  • 386
  • 3
  • 12