I am having two problems regarding compiling and running an Eclipse java project from command line. This works fine when I am just running from the eclipse IDE. I tried googling but couldn't really get the thing working. Any help is much appreciated.
Problem 1: When I try to compile from a location different from the directory where the .java file is, it throws the error "cannot read: myfile.java". But if I migrate to this directory then it compiles.
The command that I was giving is (when in some other directory):
javac -cp C:\ABC\src\XYZ myfile.java
The command that I was giving when in XYZ directory:
javac myfile.java
This generated two .class files myfile.class and Testing_Thread.class(I guess this because I have a thread in my code)
Problem 2: After I have compiled by going to its directory, when I try to run the program, I get the error "Exception in thread "main" java.lang.NoClassDefFoundError: myfile (wrong name: XYZ/myfile.java)" even when I am trying to run from the XYZ directory. I get the same error when I try to run from some other place also.
The command that I was giving when in XYZ directory:
java myfile
The command that I was giving when in some other place:
java -cp C:\ABC\src\XYZ myfile
I am also attaching a hierarchy of my directory structure if it is of any help:
These examples assume the following source structure:
C:\temp\compile-test\src\a\b\c\D.java
Where D.java is:
package a.b.c;
public class D { }
The first problem, cannot read: myfile.java, is because it is not correct to use the cp command line option to point to your source code.
C:\temp\compile-test\src>javac -cp c:\temp\compile-test\src\a\b\c D.java
javac: file not found: D.java
Usage: javac <options> <source files>
use -help for a list of possible options
This should instead be the following, where javac is run from your source folder, and we can use relative paths to the source files (NOTE - javac is run from the source folder here):
C:\temp\compile-test\src>javac a\b\c\D.java
Or this, where we specify full paths to the source files, and javac can be run from anywhere (NOTE - javac is run from C:\ here):
C:\>javac temp\compile-test\src\a\b\c\D.java
Both of the above options will result in your class files being created in the same folder as the source. I.e.:
C:\temp\compile-test\src\a\b\c\D.class
For the second problem, if you try and run a class that has a package name from 'inside' the package, this will result in the name being wrong (NOTE - java being run from 'inside' the package here):
C:\temp\compile-test\src\a\b\c>java D
Exception in thread "main" java.lang.NoClassDefFoundError: D (wrong name: a/b/c/D)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: D. Program will exit.
To run the D class, you should be at the package 'root', and supply the Fully Qualified Class Name. I.e.:
C:\temp\compile-test\src>java a.b.c.D
Exception in thread "main" java.lang.NoSuchMethodError: main
Note I get an exception as the D class doesn't have a main method, and so cannot be run. To fix, we add a main method:
package a.b.c;
public class D {
public static void main(String[] args) {
System.out.println("main");
}
}
and re-run:
C:\temp\compile-test\src>java a.b.c.D
main
Related
I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp
JAR file will not run, instead returns
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
I've tried deleting the class file, then re-creating the JAR file. I've tried deleting and re-creating the program from scratch. I've tried creating the JAR file without a manifest.
package ExampleProject;
public class ExampleProgram {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is the exact process in how I tried to create the JAR file on the command-line.
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java
Josephs-MBP:program jepappano4$ java ExampleProgram
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java
Josephs-MBP:program jepappano4$ ls
ExampleProgram.class ExampleProgram.java manifest.mf
Josephs-MBP:program jepappano4$ jar -cvfm myprogram.jar manifest.mf *.class
added manifest
adding: ExampleProgram.class(in = 450) (out= 301)(deflated 33%)
Josephs-MBP:program jepappano4$ java -jar myprogram.jar
Error: Could not find or load main class ExampleProgram
Caused by: java.lang.NoClassDefFoundError: ExampleProject/ExampleProgram (wrong name: ExampleProgram)
I am following a tutorial on creating JAR files and I expected this process to work, but it doesn't. What am I doing incorrect here?
Not sure if this is important or not, however, I am using Java 11.
The 'wrong name' error is the key error here. This is what it means:
In java, the full name of a class is its package, followed by a dot, followed by the classname. So, your full name for this class is ExampleProject.ExampleProgram.
To start them, you must put this full name in yor manifest: It needs to have an entry that looks like:
Main-Class: ExampleProject.ExampleProgram
In addition, folder structure must match the dots. So, the jar file (which is really just a zip file) must contain inside it at the root level a directory named ExampleProject and this dir needs a file named ExampleProgram.class.
You've gotten one or both of those wrong, causing this error. You can check the structure of your jar file as follows:
jar tvf myjar.jar
you should see a listing for:
ExampleProject/ExampleProgram.class
in that precise location.
I have a problem while trying executing my java application.
Whenever I try to execute the program through the command
java ProgAudioJ
I get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
at java.lang.ClassLoader.defineClass1(NativeMethod)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(NativeMethod)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ProgAudioJ. Program will exit.
If I remove from my code:
package es_2011;
Everything works perfectly. How do I solve the problem?
Because I found these answers unclear, here is what you need to do.
First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):
cd /myFolder/myPackage
javac MyClass.java
cd ..
java myPackage.MyClass
Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like
package com.somedomain.someproject;
you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):
cd /myCode
java com.somedomain.someproject.MyMainClass
Try using:
java es_2011.ProgAudioJ
(instead of java ProgAudioJ).
I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.
Try this (compile and run):
dir
2011-02-10 00:30 <DIR> .
2011-02-10 00:30 <DIR> ..
2011-02-10 00:27 58 es_2011
javac es_2011/ProgAudioJ
java es_2011.ProgAudioJ
It's quite clearly stated there:
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,
src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;
(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.
Try this,
Compile your class using below command
$ javac ProgAudioJ.java -d .
Run your application by command
$ java es_2011.ProgAudioJ
The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.
I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file.
It has been packaged up as
es_2011/ProgAudioJ
In order to run it - you have to either move up a directory:
If you are here: (Windows)
src\es_2011\
move to
src\
Then run the line:
java es_2011.ProgAudioJ
This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).
I can compile this JNA example code (from step 2 of https://github.com/twall/jna/#getting_started):
package com.sun.jna.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
...using javac -classpath .:jna.jar -g HelloWorld.java without error. (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.)
But when I run it using java -classpath .:jna.jar HelloWorld, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/sun/jna/examples/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
I get the exact same exception on Mac OS X and Linux.
How do I get this to run?
This example (as well as vast majority of java classes) uses packages:
package com.sun.jna.examples;
In order to compile / run it properly you need to run javac / java from the "root" folder (e.g. folder where "com" is located):
Let's say you have a folder called examples. You'd put both the jna.jar and the source code in it preserving folder structure:
/examples
jna.jar
/com
/sun
/jna
/examples
HelloWorld.java
You compile and run using:
javac -classpath .:jna.jar -g com/sun/jna/examples/HelloWorld.java
java -classpath .:jna.jar com.sun.jna.examples.HelloWorld
Note the path separators in the former case and dots in the latter.
Either just remove this line and recompile (which is fine in this case as you just try out some sample)
package com.sun.jna.examples;
or read up on what packages in java are and how they have to be handled (ChssPly76s Posts as a starter).
Better choose the second option as sooner or later (probably sooner) you will have to deal with packages anyway. So just take the time now to read up on it.
In Eclipse, under Java Build path > Order and export, select export jna.jar.
I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello;
public class Hello
{
public static void main(String[] args)
{
coucou();
}
public static void coucou()
{
System.out.println("Hello there");
}
}
In a separate project called Tool, I want to be able to call the method Hello.coucou() so I wrote something like this:
import hello.*;
public class Tool
{
public static void main(String[] args)
{
System.out.println("main program running");
Hello.coucou();
}
}
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong.
Any ideas?
Cheers
David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar:
$ java Tool -classpath .:./extern/Hello.jar:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
You need the Hello.jar on the classpath when you run as well as when you compile.
Actually the trick was in the order of the arguments in the command line:
Is the -cp (or -classpath) is set last, then it doesn't work
java Tool -cp .:extern/Hello.jar
It has to be first like:
java -cp .:extern/Hello.jar Tool
!!!
Java uses dynamic late binding, so putting the JAR in the classpath during compilation is only necessary to ensure that your code is using the classes from it correctly, but it does not actually embed them into your code as the linker would in C/C++. Thus, you need to set the classpath also when executing the code.
However, this:
$ javac Tool.java -classpath .:./extern/:
should not work either, since JARs need to be put into the classpath directly, not just the directory they live in:
$ javac Tool.java -classpath .:./extern/Hello.jar
Finally, you are placing your code in the default nameless package. This is OK for fooling around, but will cause problems in the long run (for one thing, you cannot import classes FROM the default package anywhere else).
When you run Java you must add the jar file too (adding the directory path only does not work).
See classpath information.
It should be something like this:
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
You need to include the Hello.jar file in the classpath when you launch it too.
java -cp xxx.jar hello where xxx is the jar you want to have in your classpath, if you want multiple jars then separate them using ;
karl