Class names, 'Introductions', are only accepted if annotation processing is explicitly requested - java

I got this error messaje while trying to copile a simple Java program. I know there is this question already here on Stack but the solution(that i forgot to include the .java suffixwhen compiling the program) still doesn't work for me. This is the program:
import java.io.Console;
public class Introductions {
public static void main(String[] args) {
Console console = System.console();
// Welcome to the Introductions program! Your code goes below here
String firstName = "Paul";
console.printf("Hello, my name is %s\n", firstName);
console.printf("%s this is learning how to write Java\n", firstName);
}
}

There are two different commands to compile Java programs and run Java programs. You're receiving an error that means that the command to compile the program (javac) has received arguments that don't include the .java suffix, as mentioned in other questions about this error.
It sounds, however, like you are compiling correctly, using javac Introductions.java. Your problem is actually in the second step, running the program. Running javac Introductions tells Java you want to compile again, and so it correctly points out that you forgot the extension.
But you're not wanting to compile a second time; you want to run it! That uses java instead of javac.
To compile: javac Introductions.java
To run: java Introductions
See also How do I run a Java program from the command line on Windows? (though this isn't really Windows-specific).

Related

Error: Could not find or load main class in Textpad 8

I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214

Is it possible to run Java program from command line without compiling?

I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.
The instructions state:
Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:
$ java JavaClassName 4 7 file.csv
program output here
My question is:
Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd
$ java JavaClassName 4 7 file.csv
would output something?
Sidenote: Currently, running that CMD outputs
Error: Could not find or load main class JavaClassName
Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.
You ask:
Isn't this skipping the compile process?
Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.
from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.
e.g. writing HelloWorld.java file
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
we can run it as
java HelloWorld.java
We can also add more classes in the single file.
from Java 11 we can also use shebang files
we have to specify which version of the java language you want to use
we have to save the file without .java extension
remember to give executable permissions chmod +x HelloWorld
writing HelloWorld file
#!/path/to/java --source 11
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
and we can run it as
./HelloWorld

Having Trouble Running Java Program in CMD

Hello stackoverflow community!
I am in the beginning of my journey of becoming a programmer and currently in the process of learning Java. I have strictly been using Eclipse to compile my programs. However, when I try to run the program through the command line I get:
"Error: Could not find or load main class FirstProg."
I've read through some other discussions on the forum and experimented with different methods, but I cannot get it to execute the program.
The path to my program (FirstProg.java) is as follows: C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing
When I enter "javac FirstProg.java" in the CMD it compiles the program and I can see the FirstProg.class generated in the path folder, but when I try to execute, "java FirstProg," it throws the error.
I tried doing things like "java com.smj.programmingByDoing.FirstProg" along with other variations but so far nothing has worked. Obviously I am doing something wrong. Please help!
public class FirstProg {
public static void main(String[] args) {
System.out.println("Mr. Mitchell is cool.");
}
}
The program runs fine in Eclipse btw.
here's a sample way of doing
create following class MyTest.java under c:\com\test folder
package com.test;
public class MyTest
{
public static void main(String[] args)
{
System.out.println("test fle");
}
}
now when you are compiling make sure that you use option -d
run following
cd \com\test
javac -d . mytest.java
next from same folder (com\test),
java com.test.MyTest
Step 1: Write Java Program.
Step 2: Compile java file to class file and generate byte code.
Step 3: Byte code translate to machine code and run on JVM.
Steps to write, compile and run java program using command prompt.
(i). Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension.
(ii). Open Command Prompt.
(iii). Navigate to the correct folder.
(iv). Set path.
(v). Compile the program.
Example:javac JavaClassName.java
(vi). Run the program.
Example:java JavaClassName
Visit the good blog to read all steps with example and images:
https://javatutorialdetails.blogspot.in/2017/10/how-java-program-work-step-by-step-in.html
Run your class after setting classpath:
set classpath=%classpath%;.;
java com.smj.programmingByDoing.FirstProg
C:\Users\smj7v\workspace\LearningJava\src> javac com\smj\programmingByDoing\FirstProg.java
C:\Users\smj7v\workspace\LearningJava\src> set classpath=%classpath%;.;
C:\Users\smj7v\workspace\LearningJava\src> java com.smj.programmingByDoing.FirstProg

Executing error in java

The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.

How to know the commands being passed when compiling with eclipse

I am new to Java programming, especially with eclipse. I would really like to know how the java programs are actually getting compiled, it would help me in knowing the Java command line interface better. So, I want to know if there is a way to know exactly which commands are being sent along with the switches to compile and run the java program?
I am sure that you are taking up a J2SE edition of Java programming which should be a fundamental to you. Let me start with giving you an glimpse of a Java Program:
class Greetings
{
public static void main(String args[])
{
System.out.println("The first character of the command line " + args[0]);
}}
So let's go thru what I meant by each line.
The class indicates beginning of the class which would contain all of
the methods to be into.
The "public static void main" indicates the main method which would contain the method to be executed.
And the characters found in the brackets of the only method is known as parameters.
Those parameters are passed from the command line to the program where they could be worked on depending on the program.
And with this, let me advise you to take up a read on this page for more on Java (J2SE).
http://www.w3resource.com/java-tutorial/java-program-structure.php
Happy Coding!
Unless there are some Eclipse logs that track that (check your installation folder), I doubt Eclipse shows it to you through the IDE. Compiling and running java mostly comes down to two commands:
javac
You compile by executing
javac -cp <your classpath/libs> <your source folder>
java
You run your program by executing
java -cp <classpath> <Class with main method> <main method arguments>

Categories