Imagine that I have two classes (shown below). Now imagine that I am compiling them using javac.exe from the command line. They won't compile because class A needs class B's methods to exist and vice versa. Is there any trick to getting them to compile from the command line? (Eclipse can compile this no problems!)
I should add they are both currently in two separate .java files.
public class A {
public void doAWork() { /* A work goes here. */}
public void doBWork() { new B().doBWork(); }
}
public class B {
public void doBWork() { /* B work goes here. */}
public void doAWork() { new A().doAWork(); }
}
It looks like your issue is elsewhere.
I can perfectly compile the code in Java 1.5, 1.6 and 1.7 with the following command:
javac A.java B.java
Even providing a single file name works perfectly, since B.java is in the same directory:
javac A.java
Are you sure the two files are placed in appropriate directories?
Related
I have created a simple java file having compilation error(Removed ; in 4th line).
public class Test {
public static void main(String args[])
{
System.out.println("Hi")
}
}
After saving If I see bin folder I can see class file(Test.class) being created.Whereas if we compile the same java code through windows command prompt class file is not created.
Eclipse generated compiled class file (below)
public class Test
{
public static void main(String[] paramArrayOfString)
{
throw new Error("Unresolved compilation problem: \n\tSyntax error,
insert \";\" to complete BlockStatements\n");
}
}
Can you please let know why we see 2 different behavior for the file having compilation error in it.
Eclipse's focus is allowing you to do software development. The behavior you've seen allows you to e.g. start unit tests on parts of the class that doesn't have compile errors to check if existing behavior is still the same at that part of the class while you refactor other parts or add new functionality.
I have two files, app.java and test.java
They both reside in the same package, and they compile just fine with "javac app.java test.java"
Two class files are then created.
However, when I go to run them with the command "java app" because app has the main method, I get "Error: Could not find or load main class app"
app.java:
package working_directory;
public class app {
public app() {
}
public static void main(String [] args) {
test testing = new test();
System.out.println(testing.calculate(60));
}
}
This Is the test.java
package working_directory;
public class test {
public test() {
}
public int calculate(int x) {
return (int) x * x * x;
}
}
Make sure to choose the right path for compilation and running code:
D:\
+--Folder(start cmd here)
+---working_directory
+----app.java
+----test.java
How to compile
D:\Folder\>javac working_directory\*.java
How to run
D:\Folder\>java working_directory.app
To use the java command, you must specify the fully qualified name of the class you want run. This means that you need to specify the package name as well.
You should run this:
java working_directory.app
Since working_directory is the package name.
You must provide a classpath, when running it from command line:
(for windows)
java -classpath . app
You have a package name declared in other words that's a folder. Your project should look like this then
C:\YourProject
C:\YourProject\working_directory
C:\YourProject\working_directory\app.java
Your Project starts at root level so it's C:\YourProject there you have to use the command line and type java working_directory.app
So I have two java files, Print.java and StaticImport.java, in src/com/test.
StaticImport.java:
package com.test;
import static com.test.*;
class StaticImport {
public static void main(String[] args) {
System.out.println("Hello world");
Print.print("This is cool");
}
}
Print.java:
package com.test;
public class Print {
public static void Print(String command) {
System.out.println(command);
}
}
So basically there is the StaticImport class that uses Print class.
How can I compile the StaticImport with javac in command line?
I have tried for example: javac -cp /home/pathToProj/ StaticImport.java, but with no success.
In java, the classpath contains class files, not java code.
First, you need to compile Print.java, since you need it to be on your classpath. Then you need to set the classpath for the compilation of StaticImport to be the directory containing the "com" directory above Print.class.
You can also compile both files at the same time, using a single call to javac.
However, the best thing to do is to use either maven or gradle to build it for you. They look after your classpath, and do a million other things besides.
I'm learning java packages in school and I'm able to create and use my package on netbeans perfectly but cannot do it from the lubuntu command line. I get an error: could not find or load main class. Here is the code but I know this is not the problem since it works perfectly in netbeans
package animals;
public class MammalInt implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public static void main(String args[])
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
package animals;
interface Animal
{
public void eat();
public void travel();
}
I first compile Animal.java and put the Animal.class file into a directory
animals. I then compile MammalInt.java. If I do not put the Animal.class file in a animals directory it will not compile MammalInt.java . After I have both class files into the animals directory I do java animals/MammalInt and get the error: cannot find or load main class. I also have doe java MammalInt and get the same error. This is really frustrating. Please help.
When compiling (a set of files) you need to use the path.So use /
javac animals/*.java
When running the Java class, you need to specify the Java name of the class.
In your case this is done as follows:
java animals.MammalInt
This says you want the class MammalInt in the package animal. Depending on your installation you also need to add your current directory to your classpath (this is where java looks for .class files), resulting in:
java -cp . animals.MammalInt
Note that you run all commands from the root of your source code tree. This means the directory that contains the directories for your packages. So if you have the following direcotries:
project/
project/animals/
project/animals/Animal.java
Then run the commands from the project/ directories.
I have written the following code:
package abc.def;
public class test {
public void test() {}
public void disp() {
System.out.println("in disp");
}
}
then I used following command to compile:
javac -d . test.java
it works fine, but when I tried to import the class "test" using "import abc.def.*" it does not import test class, the code is :
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
following error is generated:
D:\javaprograms>javac checktest.java
checktest.java:8: cannot access test
bad class file: .\test.java
file does not contain class test
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
test t = new test();
^
1 error
I also had the same problem.
No additional classpath is required to set.
According to your scenario, your working directory might contains test.java file. You can just remove the test.java file from the working directory and compile using javac checktest.java.
It will work.
Thanks.
Britto
Did you make the proper directory structure? You need to have the test.java file in abc/def if that's the package name you want.
You can also point to the compiled test.class file with -cp flag
Example:
javac -cp test checktest
Your directory structure should look like this:
current working directory
checktest.java
abc
def
test.java
Then, from the directory on the top, you can compile checktest:
javac checktest.java
This will automatically find (and compile) test.java too. If you only want to compile test, do it this way:
javac abc/def/test.java
Then all the class files will be in the right directories, too.
It seems that you have by mistake compiled test.java in the topmost directory itself, therefore the JVM is picking test.class from the top most directory and also from abc\def\test.class hence conflict is happening.
please type: ls test* in the top most directory and confirm if that is the case and delete this extra test.class and then recompile.
first know this - To use the package in other programs, compile the .java files as usual and then move the resulting .class files into the appropriate subdirectory of one of the directories referenced in your CLASSPATH environment variable.
For instance if /home/name/classes is in your CLASSPATH and your package is called package1, then you would make a directory called package1 in /home/name/classes and then put all the .class files in the package in /home/name/classes/package1.
Now suppose your classpath is /home/name/classes then compile
package abc.def;
public class test {
public void test() {} public void disp() { System.out.println("in disp"); }
}
using $ javac -d /home/name/classes test.java
Now put this code
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
inside the folder