What i can't run a class file created Intellij Idea? - java

Have a simple programm:
package com.test;
public class Main {
public static void main(String[] args) {
System.out.println("lol");
}
}
In Intellij Idea project run correctly, but if i run out class file by cmd, like java Main.class i have a Error
Error: Could not find or load main class Main.class
If i compile .java manually - i have some error.
In $PATH path to .../jdk/bin. In Intellij Idea path to SDK .../jdk.
What's the problem?
Thanks!

Your class is not Main, but com.test.Main. You should use:
java com.test.Main
from the root directory of the compile output (i.e., from the same place as the com/ folder)

Related

.class file not being created in Sublime Text: Java

I am starting to learn Java and decided to use sublime text. I prepared a build system that would allow me to both compile and run the program. When I created my first file, it gave an error:
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
My code:
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Helloworld")
}
}
The name of this file is Helloworld.java. I noticed one thing that the directory in which I saved this file didn't have the main class file. I am confused. Please help.
Build system code:
{
"shell_cmd": "java $file_base_name"
}
javac $file_base_name (note the c) will compile your source file; java com.foo.YourClass will run it - note that the java command does not take a filename; it takes a class name.
More generally once you introduce packages and dependencies these scripts become impossible. Instead, use a build system and then if you still insist on using SublimeText (the vast majority of java programmers use IDEs such as Eclipse or IntelliJ), you can put mvn compile or mvn run in your compile/run scripts.

Cannot run class files compiled by eclipse in command line

I compiled classes using eclipse, but when I try to run them in the command line, it returns "Cannot find or load main class".Command line.
My class is:
package Chapter10;
public class Hfpd10
{
public static void main(String[] args)
{
System.out.println("it works");
}
}
I am running the command from within the C:\Users\John\Documents\Java\EclipseWorkspace\HeadFirstDesignPatterns\bin\Chapter10 folder, where the class files are. The command is:
java Hfpd10
It runs in eclipse and I used the exact same path.
Eclipse path
The only question I could find like this went unanswered. cannot run java file in command line that created by eclipse
Change to the parent directory and run the following command:
java Chapter10.Hfdp10
This required is because your class is in a package and you need to reference it with its full package name.

Can not find or load main class - classloader issue

I know this question has been asked before, but I’ve read through most of them and still can’t figure out my problem.
I’ve compiled the below code into "HelloWorld.class" located in the directory shown below.
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
But I cannot get the .class file to run using the Java command on Window Command Prompt:
I believe my PATH and CLASSPATH are set correctly (as shown below). But I still can’t find the class. Any ideas why?
The name of the class is not HelloWorld. It goes like:
java helloworld.HelloWorld
and you have to call that from the directory above helloworld.
In other words: the package name is part of the class name. When you invoke java; you have to provide that full name; and the classpath needs to point to the directory that contains that package.

java.lang.NoClassDefFoundError with jar file in terminal

In Eclipse, I wrote a Java class Test with a main() function.
The project in which is defined the class, I added the jar file bcprov-jdk15on-151.jar (I am using the library BouncyCastle).
In Eclipse, there is no problem and my program runs normally. But when I try to do it in a terminal, I get an exception.
After checking SO I found a similar post: NoClassDefFoundError while running a java program in terminal from IDE but the solution given doesn't work.
To illustrate my case, in the directory C:\Docs\workspace\Terminal\bin\ I have the file Test.class. If I run java Test I get Exception in thread "main" java.lang.NoClassDefFoundError: org.bouncycastle.math.ec.ECFieldElement.
If I run java -cp bcprov-jdk15on-151.jar Test (I put the .jar in the same directory to simplify) I get Error: Could not find or load main class Test so it seems that the dependency error is solved but another one occurs.
What am I doing wrong? Just to give the structure of my .java file:
import java.io.*;
...
public class Test {
... local methods ...
public static void main(String[] args) {
...
}
}
Thanks in advance.
Try this, you forgot to include current path "."
java -cp ".;bcprov-jdk15on-151.jar" Test
Hope it help

Error package does not exist java

I am trying to import a package I created, my file structure is as follows:
javacode
tester.java
mypackage
Cram.class
In my tester I import the package using: import mypackage.Cram;
but when I try to compile my tester.java I get the following
error: package mypackage does not exist. Any help would be much appreciated. Here is the error; algo is my tester.java and pack is mypackage.
algo.java:6: error: package pack does not exist
import pack.Cram;
^
If you are compiling all of your files at the same time, then you should just be able to use your code as-is, provided that you have your classes defined properly and in the directory structure as indicated by your package keyword.
Suppose you have the following directory tree (as in your original post):
javacode
Tester.java
mypackage
Cram.java
classes
<.class files will be placed here later>
And the following classes defined:
Tester.java:
import mypackage.Cram;
public class Tester {
public static void main(String[] args) {
Cram c = new Cram();
c.doSomething();
}
}
Cram.java:
package mypackage;
public class Cram {
public void doSomething() {
System.out.println("Hello from Cram!");
}
}
You can compile all of these files into a single directory (usually, IDE's will compile to a "classes" or "bin" directory) with the following command:
> javac -d classes Tester.java mypackage\Cram.java
This will place all your class files in the directory "classes"
classes
Tester.class
mypackage
Cram.class
You can then run by using:
> java -cp classes Tester
And produce output:
Hello from Cram!
If you are compiling your package separately from Tester.java, like a library, then you can do the same thing, just with some separate commands.
Compile mypackage.Cram:
> javac -d classes mypackage/Cram.java
This will put the .class files in the same classes directory. When you try to compile Tester.java (which uses mypackage.Cram), you simply tell the compiler where your classes are:
> javac -d classes Tester.java
and then run:
> java -cp classes Tester
produces the same output.
My guess is, your classes and names are all mangled, and Java expects them to follow a convention.
When you compile, it is looking for .java files. Put the Cram.java file in there, and try compiling again.
Change your import in Algo.java. You are looking for pack.Cram, and you have indicated it is mypackage.Cram.

Categories