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
Related
I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!
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".
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
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>
Running basic java programs from commands line is a 3 steps process:
Write code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Compile by javac HellWorld.java which would check for errors & generate HelloWorld.class file.
run code by giving the class name --> java HelloWorld
Now,
I am curious to know why:
java HelloWorld works but when we give fullpath of the classfile, it throws an error
$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
What does it make a difference if we give just the classname Vs classname with file-extension?
What does it make a difference if we give just the classname Vs classname with file-extension?
The argument you give to the java binary isn't meant to be a filename. It's meant to be a class name. So in particular, if you're trying to start a class called Baz in package foo.bar you would run:
java foo.bar.Baz
So similarly, if you try to run java HelloWorld.class it's as if you're trying to run a class called class in a package HelloWorld, which is incorrect.
Basically, you shouldn't view the argument as a filename - you should view it as a fully-qualified class name. Heck there may not even be a simple Baz.class file on the file system - it may be hidden away within a jar file.
What does it make a difference if we give just the classname Vs classname with file-extension?
It is because that is the way it is. Sun / Oracle have implemented the java command to work that way since Java 1.0, and changing it would be massively disruptive.
As Jon says, the argument to the command is a fully qualified class name, not a filename. In fact, it is quite possible that a file with the name HelloWorld.class does not exist. It could be a member of a JAR file ... or in some circumstances, just about anything.
In Java 11 and later it is also possible to compile and run a single Java source file with a single command as follows:
java HelloWorld.java
(This possible because Oracle no longer supports Java distributions without a Java bytecode compiler.)
In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension.
When you run java class file after compile then run the following command:
java HelloWorld
Note: Need to setup java classpath