i have problem running JAVA program from CLI:
As you can see, i run javac Main.java to get Main.class file. And when trying to execute java Main, java can't find Main class. There is my class in Eclipse:
What am i doing wrong?
You should first compile your class then run the the compiled class and not the sources. So from command line:
d:
cd programos/xampp/htdocs/im/OpenCV/src/stitching
javac Main.java
The above command will tell the java compiler to compile the sources of the Main class and generate a .class in bytecode which can be passed to the java process to be run:
java Main
You have to go one dir up cd .. and than java stitching.Main
Because you declared your Main class to be in package stitching, what you should need to do is actually navigate to the folder above the root package (in the bin directory), then execute java stitching/Main
Related
At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)
I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.
Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:
package com.hello.programs;
public class ABC {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
Later, run it by giving the package name and then my java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.
It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?
Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.
cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java
--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs
java com.hello.programs.ABC
When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.
This must mean that either:
[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.
[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.
Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).
When you use javac ABC.java you are compiling the class, and javac places it in the current directory.
So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).
You can use the javac -d flag:
-d <directory> Specify where to place generated class files
For instance:
> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class
Would work, because javac did place ABC in com/hello/programs.
Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.
I tried to run my basic HelloWorld.class file from my terminal.
I use the following input:
Java HelloWorld.class
But it says:
Error: Could not find or load "HelloWorld.class"
I have tried giving it a directory but it doesn't work.
because you didn't compiled or run it successufully.you should use
javac HelloWorld.java
to complile and
then use
java HelloWorld
to run it.
plz check this tutorial http://introcs.cs.princeton.edu/java/11hello/
The class should be (Executable Class should definitely contain the main method with same declaration as below)
Class MyClassName
{
// Methods here
public static void main (String args[])
{
// Code here
}
}
To Compile, it should be:
javac MyClassName.java
On successful compilation, MyClassName.class would be generated in your folder.
To run, it should be
java MyClassName
In case your java is in say D:/JavaWorkDir/src, You need to compile and run from the folder D:/JavaWorkDir/src. Also Ensure that your classpath is set appropiately.
You don't run it as
java HelloWorld.class
but
java HelloWorld
There is no need of .class extension.
But note you always have to use fully qualified name. So if your class resides in some package say myPackage then you need to run
java myPackage.HelloWorld
You are receiving this error because you shouldn't include the .class when you run the compiled file.
After you've compiled:
javac HelloWorld.java
run using:
java HelloWorld
(don't do: java HelloWorld.class)
Run it as Java HelloWorld and not like Java HelloWorld.class.
The error Error: Could not find or load "HelloWorld.class" is occuring because:
The class may have not compiled correctly.
The compiled class is not available on the path from where you are trying to run it.
Proper classpath is not set for compiling and running the java classes.
Whenever you write a Java Program named HelloWorld, you must compile it as:
javac HelloWorld.java
Once the HelloWorld.class class file generated in the same directory where you have your java file, compiled by the compiler, you can run it from console as:
java HelloWorld
if u want to try hello world you can also try to run it on the NetBeans application and also on Jdoodle.com
I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments
I created a java program in Eclipse. When I run the program in Eclipse ("run as -> Java Application") the program runs fine and I have the correct output. However, when I try to run the program in the command line interface I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
Could not find the main class: HelloWorld. Program will exit.
The class file is in directory bin and I try to run it with the command:
java HelloWorld
Since your class is in the package helloworld you should run it like this:
java helloworld.HelloWorld
Also make sure "." is on your classpath.
I try to compile it with the command:
java HelloWorld
TO compile a java program you should use javac command like
javac Helloworld.java
Are you sure that the directory where your classes are is in the classpath? Typically, in your project directory, the "classes" or "lib" directory.
If you are running from that directory, you could try adding ".".
See the -cp parameter of java runtime executable.