How to compile and run java with package - java

HI I'm having trouble with my java compille
I made folder named 'Test'. In this folder i make two folders, one is src, another one is bin. Then I made Test. java in that src folder
package Test;
import java.io.*;
public class Test {
public static void main(String args[]) {
System.out.println("hi");
}
}
i saved it and back at Test folder and then i compile like this
javac -d bin src/Test.java
Thus, i have Test folder in bin folder.
finally in Test folder i write this command
java -cp bin/Test Test
unfortunately, it says can't find Test class
How can i run this code???

When your class is in a package, the name of the class includes the package. Thus Test.Test is the Test class in the Test package. -cp bin tells java that the classpath starts in bin.
java -cp bin Test.Test
# classpath main-class

"-cp" expects a directory, not a file. Give it the ./bin/ directory, not the file you're trying to execute.
java -cp bin Test.Test

Related

Unable to run javac in a method importing a class in a jar

I want to compile Entry.java which imports a Utilities JAR.
I'm trying to run javac in the Entry.java to create its class file. But every time I get the package utilities does not exist error.
I tried this command from the Project folder, but it didn't work: javac .\main\Entry.java.
The directory structure is this:
Project
Manifest.txt
main
Entry.java
utilities
Utilities.jar
Entry.java is a simple file importing Utilities (a JAR file):
package main;
import utilities.Utilities;
public class Entry{
public static void main(String[] args){
System.out.println(Utilities.word());
}
}
Utilities.java has only a method that returns a string:
package utilities;
public class Utilities{
public static String word (){
return "Utilities";
}
}
If I understood correctly if I run javac from the top directory (called Project) all the subdirectories will be in the CLASSPATH, so I don't need -cp here. I would need it if I run javac from inside the main directory.
(But I've tried anyway. But javac -cp .\utilities\Utilities.jar .\Entry.java didn't work as well)
So the whether the problem is the import or I didn't actually understand how to work with JAR files.
Or none of the above.

compiling a class with a package throws an error

I'm fairly new to java and am trying to get the basics down. One of them would be dealing with package, I'm trying to implement one class into another using package HelpPack; and upon using javac -d HelpPackage A.java it throws the error javac: file not found: A.java. Thank you for your help
//B.java
package HelpPack;
public class B{
public String name(){
return "Class b";
}
}
//A.java
package HelpPack;
public class A{
public static void main(String[] args){
B b = new B();
System.out.println(b.name());
}
}
Compile all your classes
javac -d . *.java
and run using,
java HelpPack.A
Have a look at javac tool
From the tool doc : By default, the compiler puts each class file in the same directory as
its source file. You can specify a separate destination directory with
-d (see Options, below).
and package tutorial.
-d specifies where the class files will go. Your java files need to be in that directory too and command should be
javac HelpPack/*java
Your package needs to match an actual directory.
So your class A.java should be in the directory HelpPack.
According to documentation :
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of apackage, javac puts the class file in a subdirectory reflecting the
package name, creating directories as needed. For example, if you
specify -d C:\myclasses and the class is called com.mypackage.MyClass,
then the class file is called
C:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.

couldn't find or load main class

I placed my Java code to the binfolder and try to run the code. The command javac Project.java terminated successfully, but the command java Project throws the error
couldn't find or load main class Project
This is my code:
Public class project {
public static void main(String args[]) {
}
}
You've got your error because of wrong syntax. Of course, java can't find Project class, there's no such thing declared. This is a correct declaration:
public class Project {
public static void main(String args[]) {
System.out.println(args[0]);
}
}
Note, that in Java class names start with upper-case letter, and access modifiers - with lower, like public, private, etc. I strongly suggest you to read Java Naming Conventions before writing any code.
If you're getting error like
couldn't find or load main class Project
there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions ), so you need to put in on the classpath with -cp option (as it mentioned by #Nikhil B). Note, that doing
javac -classpath "c:\java\jdk1.7.0.45"\bin" Project.java
which you posted in comments to his answer isn't correct. You should tell java interpreter where to find .class files, not java compiler (+ as I see, you've compiled your .java file just fine).
So, put the directory which contains .class file to a classpath somehow like this:
[root#crmdev clarify]# pwd //shows current directory
/home/clarify
[root#crmdev clarify]# javac Project.java //compiles .java file
[root#crmdev clarify]# ls Project.* //here are my test files for your case
Project.class Project.java
[root#crmdev clarify]# java -cp . Project "hello, #user5779261" //executing test code
hello, #user5779261
Run the java command with classpath option and it should run. (and change the class name to project from Project)
java -classpath "path to bin directory in double quotes" project

Error java executing: Couldn't find or load the main class

I'm trying to execute a simple java code (I have already compiled it with no problems) but it gives me the next error:
c:\Users\alejandro\Desktop> java HelloWorld.java
Error: Couldn't find or load the main class.
The code is the next:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world!");
}
}
I have set the PATH variable correctly.
I have deleted CLASSPATH variable.
I have both files (.java and .class) in my Desktop.
You're specifying the name of the source file. That's not what you provide to the java command - you specify the class name.
java HelloWorld
This assumes that HelloWorld.class is somewhere on the classpath, which would default to "the current directory".
If you had a package, e.g.
package foo;
public class HelloWorld {
...
}
Then you would want to put HelloWorld.java in a directory called foo, and compile and run from the root directory:
> javac foo\HelloWorld.java
> java foo.HelloWorld
Note how now the fully-qualified class name is foo.HelloWorld, not foo\HelloWorld.
when you run the compiled file, you should use only the class name. The compiled file will have an extension of .class but you should not add any extension. Just use the class name.
change
c:\Users\alejandro\Desktop> java HelloWorld.java
to
c:\Users\alejandro\Desktop> java HelloWorld

package in .java file makes class file unuseable

It's been too long since I've last done Java, and I can't remember why the following happens:
Given this file, created by a standard Maven project, as can be seen here: Maven Tutorial
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Compiling this, not even with Maven but with the standard javac executable, will generate a class file without errors. Try to run the class file, and you get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: App (wrong name: com/mycompany/app/App)
Remove the package command, compile again and it runs just fine. Why is this? I'm running JDK 1.6.0_21 btw.
One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierachy of the class. In your case the package name is "com.mycompany.app" so the App.class (compiled App.java file) should reside in "com/mycompany/app" sub-directory. It doesn't matter where the source file is residing though. For example, I have copied your file and did the following:
$ ls
App.java
$ javac App.java
$ ls
App.class App.java
$ mkdir -p com/mycompany/app
$ mv App.class com/mycompany/app/
$ java com.mycompany.app.App
Hello World!
$
Please read Wikipedia page about Java Packages for more information. You can also take a look at these links:
The Java packages tutorial
Java packages tutorial
Oracle's notes on packages
Good luck!
When you attempt to execute your program, it will look for the class file using the path specified in the package. So, when you have the package statement in the file, your class file must be in the com/mycompany/app/ directory (relative to what directory you're attempting to run it from); if it can't find it, you get that exception.
Thus, when you remove that package statement, the JVM will look for it in current directory, which is why it works (because you're executing java App in the same directory in which the App.java and App.class files exist).
You need to add the com/mycompany/app folder to your Java CLASSPATH . If I remember well, you can also do it from the cmdline using the parameter "-cp".
This is because in Java filesystem files map to classes (e.g. each public class must be in a separate eponymous file) and packages map to directories.
So if you have a class which is in the com.mycompany.app package it must be in com/mycompany/app directory relative to the classpath.
In your case you should have an output directory, say and the you should have the class in /com/mycompany/app/App.java. Then you build it, running javac from and giving com/mycompany/app/App.java as parameter, instead of com/mycompany/app/App.java.
Running the class works in an analogical way, but you give the fully-qualified-name of the class, instead of the directory path.

Categories