Consider Some.java file
inside com/work folder. I have given the package name accordingly.
package com.work;
class Some{
public static void main(String... args){
System.out.println("Hello World");
}
}
I am able to compile this Some.java file and get the respective Some.class file,
But when I am trying to run the Some.class file with command java Some, considering I am in the work folder, I am getting the below error.
Error: Could not find or load main class com.work.some
Caused by: java.lang.ClassNotFoundException: com.work.some
Do not run this from the work folder. Go two levels up, then run
java com.work.Some
If this does not help, additionally set the classpath
java -classpath . com.work.Some
Related
I am trying to compile and run a .java file using classes that are present in a jar file located in a separate directory.
PROJECT
├───build
│ └───IN HERE
├───checkstyle
├───doc
│ └───yourusername
├───lib
└───src
This is the directory structure, the .jar file is in the lib directory, and my .java file is in "IN HERE". I am completely unsure of how to import the classes from the .jar to my file. I have seen both import package.name;, and simply package name;.
I have the absolute path for both "IN HERE" and "lib" within my system classpath environment variable, but nothing still seems to work.
The java file is simply:
package dnd.models;
//import java.lang.Throwable;
public class Dungeon {
public static void main(String [] arg){
//ArrayList
//private double roll;
System.out.println("hello world");
}
}
The .jar is entitled dnd.jar and supposedly has the packages dnd.die and dnd.models.
I have tried compiling and running as the following from the main PROJECT folder:
javac Dungeon.java
javac: file not found: Dungeon.java
Usage: javac <options> <source files>
use -help for a list of possible options
This seems odd to me because the classpath should prompt it to look through the classpath folders, should it not? After that I attempted to use "IN HERE" as my working directory to compile/run, but then I get this error:
javac Dungeon.java --This works
java Dungeon.java
Error: Could not find or load main class Dungeon
I have also tried with commands like
javac -cp .;MYDIRS\PROJECT\lib\dnd.jar Dungeon.java
But they do not work either. I have tried dozens of different ways of formatting these kinds of statements, so I am hoping there is something obvious I missed as this is majorly holding me up.
I would expect the java to compile and recognize the package and print hello world without any errors. Anything helps, I'm at a loss.
I pasted this code from a tutorial site and compiled the code in CMD as well as attempting to run the code:
public class ExampleProgram {
public static void main(String[ ] args) {
System.out.println("Hello World");
}
}
Since the PATH variable already existed, I added a path to the 'bin' file within the java file. I also added a new variable (CLASSPATH) and set the variable value to where I keep the .class file.
PS:
The .java and the .class file are in the same folder
No spelling or capitalization errors are made
I did not add .class at the end where I try to run the code
If you are working with "Netbeans" IDE and you want to run your program on command prompt(cmd),you should delete package statement or comment it by // marks. then use the following command:
javac yourProgName.java
java yourProgName
hope it be usefull
Assuming that you have you java path set up done coorectly, run the following commands in order :
JavaProg is the name of the java file and your public class.
1.Suppose my .java file is in Desktop, then run the following command
cd Desktop
2. Compile it
javac JavaProg.java
3. Run it
java JavaProg
Keep the .java file and remove the .class file.
Change to the directory of the .java file and compile the java file using the following command which will generate a .class file
javac ExampleProgram.java
Run the program by
java ExampleProgram
I create jar file in IDEA Build>Build Artifacts. But can't run it with java -jar jarname.jar - Error: Could not find or load main class Hello. MANIFEST.MF file is in the /resources/META-INF/ folder. And here is the launcher class:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: Hello
EDIT: Added artifacts setting screenshot
You go to project structure then choose "Artifacts" from the left tab. Add a new artifact and as you see here although I have a manifest selected and a Main class selected, on the left side it shows what it will add to the jar. On the right side it shows what's available(un-added). However, since I haven't added anything into my jar yet, it will only add the Manifest and none of the actual code.
You need to specify that you want to add the compile output to the jar or else it will only have the manifest and not your actual classes. You can do that by just double clicking on it. If you specify the directory above the compiled output, it will add the src as well I believe.
Update: Add external dependencies
i believe your manifest file must say what the main class is if you want it to auto execute.
Main-Class: Hello
otherwise you need to specify it on the command line when attempting to execute the jar. As far as how to do that with IntelliJ, I can't help you there.
java -cp hello.jar Hello
Note that the reference to the class with the main method is the fully qualified location (package.classname) but since your class has the default package, its not necessary.
If your jar file build correctly.
try java -jar hello.jar Hello
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
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.