Running Java Program from Command-line - java

I downloaded a java program that consists of two folders src and classes containing the source files and class files respectively. Now, the src and classes folders contain a several nested sub-folders wherein the last sub-folder contains the source and class files respectively. More precisely, the path to a source and class file is src/edu/univ/.java and classes/edu/univ/.class. Given that the file containing the main function is Main.java, how can I run this program from command-line.
I have tried:
java src/edu/univ/Main but I get Exception in thread "main" java.lang.NoClassDefFoundError: src/edu/univ/Main
I have also tried:
java src.edu.univ.Main but I encounter a similar error

From the root of the project:
java -cp classes edu.univ.Main
This tells the JRE that the classes directory is the root of your package hierarchy. The JRE will load packages from there, following the directory/package naming hierarchy.
http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Related

Is it necessary to write the annotation (.java) when I try to run my java program in the terminal?

Usually, we compile a java program using the javac keyword and then run it with the java keyword.
But in my case, if I just type java Main.java, it runs without any problems.
But if I try to run the program the conventional way:
javac Main.java
java Main
it returns an error:
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError
Is this supposed to happen? I had learnt the conventional method.
I have set IntelliJ as the default application for opening .class and .main files.
After you compile your code, you end up with .class files. These binary files are the bytecode that Java interprets to execute your program.
NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.
In order to solve this error, you must know how Java finds and loads the classes,
you just remember that there is an environment variable called CLASSPATH which includes directories where Java looks for all class files and if it doesn't find your main class there then it throws "Error: Could not find or load main class XXX", where XXX is the name of your main class.
For example, if you had a class com.example., after compiling you would have a class file sensor.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/
Example:-
C:\project\practice\Main.class on my computer.
C:\project\practice\ will it run? No, it will not run it will give you:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Since the name of the class is not Main, instead it’s practice.Main even though your classpath is set to the current directory.
Now, what will happen if I give command java practice.Main from C:\project\practice\ it will again not run and give an error?
Exception in thread "main" java.lang.NoClassDefFoundError: practice/Main
Why because now it looking for a class called Main which is in package practice, starting from current directory "." but don't find it since there is no directory called "practice after this path "C:\project\practice".
To run it successfully you need to go back to directory C:\project and now run
C:\project>java practice.Main and It will run successfully

Running java program through cmd ERROR: no main method found

I am trying to run a bat file to easily run my program. The Batch file is in the folder project folder, along side the bin folder.
I have many classes in many different packages. But the main method is in my Game.class which is located in:
bin\xx\xx\xxxx\xxx\xxxxxx\cards\game\Game.class
So I have set two bat files and tried them both:
1.
java -cp bin\xx\xx\xxxx\xxx\xxxxxx\cards\game Game
2.
java -cp bin.xx.xx.xxxx.xxx.xxxxxx.cards.game Game
Both times I get this error:
Could not find or load main class Game
This makes more sense:
java -cp /path/to/"class-root"/ full.qualified.name.of.Game
...where "/path/to/class-root/" is probably "bin" in your case, and relates to "default package" (no package). From there on the path should be consistent with the package(s) of your class(es)

Running a java program with multiple jar files and classes [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

NoClassDefFoundError when attempting to run from command line

Apologies in advance - I know there are many, many, many very similar questions but I wanted to ask something specific to my situation.
I have a bunch of java and jar files in the same directory. I am able to compile fine and thus end up with a number of class files in the same dir. But when I go to execute the program it gives a NoClassDefFoundError saying it can't find the specified class:
C:\Users\DB\Desktop\nextreports-integration-demo\src\ro\nextreports\integration>
java -cp ".;*.jar" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
I tried the same thing from a higher-level dir but it made no difference:
C:\Users\DB\Desktop\nextreports-integration-demo\src>java -cp ".\ro\nextreports\
integration\*.jar;.\ro\nextreports\integration" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)
The package statement in the source file is:
package ro.nextreports.integration;
I have a feeling I'm overlooking something very elementary. Thanks in advance.
Edit: Thanks very much. It works with the following:
java -cp ".\ro\nextreports\integration\nextreports-engine-6.3.jar;.\ro\nextreports\integration\commons-jexl-2.1.1.jar;.\ro\nextreports\integration\commons-logging-1.1.1.jar;.\ro\nextreports\integration\derby-10.10.1.1.jar;.\ro\nextreports\integration\itext-2.1.7.jar;.\ro\nextreports\integration\itext-rtf-2.1.7.jar;.\ro\nextreports\integration\itextpdf-5.0.6.jar;.\ro\nextreports\integration\jcalendar-1.3.2.jar;.\ro\nextreports\integration\jcommon-1.0.15.jar;.\ro\nextreports\integration\jfreechart-1.0.12.jar;.\ro\nextreports\integration\jofc2-1.0.1.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\poi-3.7.jar;.\ro\nextreports\integration\winstone-lite-0.9.10.jar;.\ro\nextreports\integration\xstream-1.3.1.jar;" ro.nextreports.integration.SimpleDemo
But why can I not use wildcards for the *.jar files? For instance, the following leads to a NoClassDefFoundError for a class in any jar file I don't make explicit:
java -cp ".;.\ro\nextreports\integration\*.jar" ro.nextreports.integration.
SimpleDemo
Suppose this is your directory tree:
src/
ro/
nextreports/
integration/
SimpleDemo.class
From your package declaration, your compiled class should be in the integration subdiretory. Check that there really is a SimpleDemo.class in that directory. If that is correct, its classpath includes the contents of the src directory.
That means that, if you didn't have any JAR dependencies, you could run your application from the src directory like this:
java -cp . ro.nextreports.integration.SimpleDemo
You need to use the fully qualified class name.
Since you do have jars, you have to include them in the classpath as well. Suppose you have one JAR in the directory above src, and another in the current directory, you could use:
java -cp ../one.jar;another.jar;. ro.nextreports.integration.SimpleDemo
If you run it from another directory, it will still work if you review the classpath's relative directories or use absolute directories to describe your classpath. I am not sure but usually the current directory is always included in the classpath by the java executable.

Can you run a .class file from terminal that is outputted by an IDE

I am trying to run a file from command line. The file is a .class file and is apart of a larger project that I compiled in Netbeans. I navigated to the .class file and ran
java MyFile
And I got:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
Caused by: java.lang.ClassNotFoundException: PersonTest.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: PersonTest.class. Program will exit
Whats up with that? (I should mention that i'm running ubuntu)
You need to check this useful link java - the Java application launcher:
By default, the first non-option
argument is the name of the class to
be invoked. A fully-qualified class
name should be used
So, you have to write the full qualified name of the class (this includes the package name).
So, the right way to execute your command is this (from the root dir where your class files are stored):
> java my.package.MyFile
Also, make sure to include all the needed dependencies at the classpath (-cp) argument (check the referenced link).
UPDATE: to include a classpath setting example:
java -classpath C:\MyProject\classes;C:\MyProject\lib\utility.jar my.package.MyFile
With this, the java runtime will search for the classes at the C:\MyProject\classes directory, and at the C:\MyProject\lib\utility.jar JAR file. You'll need not only your class direct dependencies, but the dependencies needed by the referenced files (the whole tree).
The answer appears to be in this line:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
It means you didn't type:
java MyFile
as you said in your original post, you typed
java PersonTest.class
you should have typed
java PersonTest
Yes you can, they are compiled by a java compiler. If you have the right version of the jvm (often other versions work aswell) than it can be run. The information about your error is not enough to tell what went wrong.
Your probably in the wrong folder, mistyped the classname, used a class in your code that couldn't be found, etc.
Unless your class is entirely standalone (i.e. only references java.lang classes like String), you'll need to add other classes/JARs to the classpath when you invoke Java.
The NoClassDefFoundError (which usually states the name of the class by the way, and always includes a stacktrace) indicates that an external class that was available when your class was compiled, is not available on the classpath at runtime.
EDIT based on update:
You're invoking your process incorrectly. You don't need to append the .class suffix of the file - doing so makes Java look for a file class class in a subpackage.
(P.S. you said you ran java MyFile. That's a lie, you actually ran java PersonTest.class. If you'd noted that to start with, it would have made it much easier for people to answer the question!)
Just consider this example
say I already have a folder src and I wrote in my notepad
package test.oye;
class testclass {
static public void main (String [] args)
{
int a=3;
System.out.println(a);
}
}
then what go to src folder and you ,yourself create a folder named test and inside it oye . Then put your .java file in it . Then cd src/test/oye only(in Command prompt or terminal).From there itself
javac testclass.java
cd src
java test.oye.testclass
This will work for sure.
If you don’t want to put .java file there … then just compile your .java file and get the .class file .
Now create the test folder and then oye inside it ….and put .class file inside it ….
Now go back to src …and then type
java test.oye.testclass;
according to terminal ide, android requires classes in DEX format when running them.
Try:
dx --dex --output=practice.jar practice.class
Then run using this:
java -jar practice.jar practice

Categories

Resources