How to know the commands being passed when compiling with eclipse - java

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>

Related

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

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).

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

Could not find or load main class

Since 2 days now I am trying to run the simplest program and I can't.
I run my programs from Windows cmd prompt.
Program:
public class Bla {
public static void main(String [] args) {
System.out.println("works");
}
}
Saved the source code as Bla.java.
Compiled the program with javac Bla.java --> Bla.class created. Tried to run the program with java Bla.class
I get the error:
"Could not find or load main class Bla.class"
I am not a complete newbie with java
1. I have configured my path and my classpath variable (Exact values below). Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\wbem;C:\Python27;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Skype\Phone;C:\mysql\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI\wbin;C:\Program Files\Java\jdk1.8.0_144\bin
CLASSPATH: .;C:\Program Files\Java\jre1.8.0_144\lib;
2. my program doesn't belong to any package and doesn't call any package
I call everything from my command line. I tried uninstalling and reinstalling java developer kit.Every time I get the same error. This is not the first time I have installed java or run a program in java but I haven't written something in a long time. What am I doing wrong?
Thanks
Assuming your working directory containes Bla.class, and your CLASSPATH contains a "." (among others), java should be able to find it.
However, you should call it without ".class", like this: java Bla. Otherwise, java will think you want to call a class called "class" in package "Bla".

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

compile and execute java file batch file problem

Given this .bat file :
#ECHO OFF
echo === Compiling and executing bat file ===
md C:\my_project\dir_prog\class_files
copy ProgAudioJ.java C:\my_project\dir_prog
javac -d C:\my_project\dir_prog\class_files ProgAudioJ.java
java -classpath C:\my_project dir_prog.class_files.ProgAudioJ
I'm wondering what's wrong with it.
It just doesn't work.
I mean it places the class files in the directory called class_files (so the compiling process is ok) but the program doesn't run...
Thanks for your help MAX
Do you have defined a main class within your project and do you have defined the
public static void main(String[] args) method for this class? Remember, that method gets
called when running stuff from shell...
EDIT: you might find this comprehensive overview usefull: Running a Java Program from Command Prompt # SkylightPublishing.com
HTH
I think the first problem is that you need to specify the class_files directory on the classpath.
The second problem is that you have to specify the fully qualified name of the class that you want to run. This depends on the package that you have defined in the Java source file.
So something like this should work: (I'm assuming your class is in the default package, i.e. no package)
java -classpath c:\my_project\dir_prog\class_files ProgAudioJ
Can you provide the contents of the source file?
If your code and command is correct, I would suspect a delay, related to the asynchronous execution of the javac or to the filesystem.
Try adding a pause command in order to check if problem is the rapidity of execution of the commands or if the problem is in the command.

Categories