I am facing an error when run the below java program on windows command prompt.
Basically, I am a C/C++ programmer , but i need to run a java file as a part of some testing..
Following is the content of my TestClass.java, not copied full code as it looks like some kind of path or package issue.
package parse_signature;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
public static void main(String[] args) throws IOException {
String path_of_file= "input/content.txt";
BufferedReader reader = new BufferedReader(new FileReader(path_of_file));
String line;
while ((line = reader.readLine()) != null) {
....
}
}
I am in the directory: C:/Users/Desktop/JavaPRGs/Test/, when i used the command javac TestClass.java,
TestClass.class got created and when i run java TestClass
I get following error:
Error: Could not find or load main class TestClass
Caused by: java.lang.NoClassDefFoundError: parse_signature/TestClass (wrong name: TestClass)
I tried going back to previous directory C:/Users/Desktop/JavaPRGs and ran java Test.TestClass
got the same error (only this difference wrong name: Test/TestClass)
Also tried follwing, but the result is same.
C:\Program Files\Java\jdk-12.0.1\java -cp . Test.TestClass
You need to remove the package and try to compile and run it again using
javac TestClass.java and java TestClass
Related
I have been trying to compile my Java code using the format javac Main.java but for some reason the compiler says that my package does not exist and as a matter of fact it is in the project structure, here is a screenshot:
The exact error is: Main.java:1: error: package com.fasterxml.jackson.databind does not exist import com.fasterxml.jackson.databind.ObjectMapper;
And my code looks like this in my Main.java:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
public final class Main {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: Main [file path]");
return;
}
UdacisearchClient client =
new UdacisearchClient(
"CatFacts LLC",
17,
8000,
5,
Instant.now(),
Duration.ofDays(180),
ZoneId.of("America/Los_Angeles"),
"555 Meowmers Ln, Riverside, CA 92501");
Path outputPath = Path.of(args[0]);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.writeValue(Files.newBufferedWriter(outputPath), client);
System.out.println("Wrote to: "+ outputPath.toAbsolutePath());
UdacisearchClient deserialized = objectMapper.
readValue(Files.newBufferedReader(outputPath), UdacisearchClient.class);
System.out.println("Deserialized: " + deserialized);
}
}
The whole code is supposed to compile like this javac Main.java and then java Main client.json. When I try to compile it by going to Run, Edit Configurations and by adding client.json as the argument of my program it works like a charm, my object is serialized as a json object in the client.json file but when I compile using command line it says no package is found. The same error happens for any other dependency I try to use. It should be noted that when I instantiate objects from my dependency it looks fine as the import lines related to those objects aren't red. So I guess my issue resides in my command line compilation or my Intellij environment. I have tried many of the solution proposed online but the problem remains. I would like some help please.
It turns out the solution was simple.
First compiling the libraries inside the lib folder and Main.java doing :
javac -cp ".;lib/*" Main.java
Then running my class Main (containing my main function):
java -cp ".;lib/*" Main
I was missing on the dot "." and the semicolon ;!
I'm executing the following code in Navicat:
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "util"
AS
import java.io.*;
import java.lang.*;
public class util extends Object
{
public static String exec(String cmd)
{
Runtime.getRuntime().exec(cmd);
return "";
}
}
And it fails with:
ORA-29536: badly formed source: Encountered "<EOF>" at line 1, column 16.
Was expecting:
";" ...
, Time: 0.059000s
The source code compiles correctly with javac, why won't it work in Oracle19?
If you compile it you get the exception:
ORA-29535: source requires recompilation
util:7: error: unreported exception IOException; must be caught or declared to be thrown
Runtime.getRuntime().exec(cmd);
^
1 error
You can fix that (and a few other minor things that don't affect compilation but aren't necessary to include, and you probably don't want to use a quoted identifier):
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED util
AS
import java.io.IOException;
public class Util
{
public static String exec(String cmd) throws IOException
{
Runtime.getRuntime().exec(cmd);
return "";
}
}
Then it compiles Oracle 18 db<>fiddle.
Note: this function is a huge security issue for your database and you probably should find an alternative solution rather than allowing arbitrary code execution.
When I run this code from eclips it will give the error print:
package Chapter08_JavaIOFundamentals;
import java.io.Console;
public class Echo {
public static void main(String []args) {
Console console = System.console();
if(console == null) {
System.err.println("Cannot retrive console object - are you running your application from an IDE? Exiting the application ... ");
ystem.exit(-1); }
console.printf(console.readLine());
}
}
But when i try to run it from a command line like this.
javac Echo.java //(this will gives no errors)
java Echo
it will give a error:
Error: Could not find or load main class Echo.
Other programma's like Hello World give no problem.
What is the problem here?
You have to call java -cp . Chapter08_JavaIOFundamentals.Echo from the parent directionry of Chapter08_JavaIOFundamentals
You need to check that you added the location of your .class file to your classpath. If it is in the current folder then you can a dot . in your classpath
So when i try to open a java class that's not in a package from the command prompt it all works fine, but when I try to open a class that's in a package it gives me NoClassDefFoundError.
And when I list the package when I try to open the class (java somepackage/someclass) it says that it can't load or find the main class.
Any help?
What I can infer is, you have two classes:
Test.java:
// no package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
so you can compile and run it using:
javac Test.java
java Test
Another class:
Test.java:
package test; // package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
And thus doing same thing gives you error.
For this you need to be in the parent directory of 'test' folder in your terminal or cmd and use:
java test.Test
No problem with compiler. You can compile as usual using javac Test.java from inside 'test' folder.
NoClassDefFoundError means that your JVM can't find your class at runtime. This could be because it's not visible (set to private or protected, or just no access modifier).
It could also be missing from your build path
package pkg1;
public class Dataguise{
public static void main(String [] args){
System.out.println("My name is Maninder Singh");
}
}
Suppose this is my code. My package name is pkg1.
1. First you need to create pkg1 dirrectory if not existed.
2. Run "javac Dataguise.java" command
3. It will generate the "Dataguise.class" file and move this file to "pkg1" folder
4. Now run "pkg1.Dataguise" command it will work.
I was having the same issue so sharing my experience.
What I assume is, you are creating a package and a class inside it, for example.
package com.vishwa.hello.commandLineArgs;
public class ComandLineArguments {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < args.length; i++) {
System.out.println("Command line arg is "+args[i]);
}
}
}
When I try to compile and run the above code in command prompt (from the package folder) as
javac ComandLineArguments.java.
java ComandLineArguments 123 456.
You get the below error:
Error: Could not find or load main class ComandLineArguments
Caused by: java.lang.NoClassDefFoundError: com/vishwa/hello/commandLineArgs/ComandLineArguments (wrong name: ComandLineArguments)
There are 2 ways to solve this:
Go to the root path of your project and then run.
java com/vishwa/hello/commandLineArgs/ComandLineArguments 123 456
789.
Command line arg is 123.
Command line arg is 456.
Command line arg is 789.
If you want to run the program from the package directory, you need to specify the class path along with the complete package
reference.
java -cp
/Users/16399/Documents/workspace-spring-tool-suite-4-4.12.1.RELEASE/CoreJava/src/
com/vishwa/ hello/commandLineArgs/ComandLineArguments 123 456 789.
Command line arg is 123. Command line arg is 456. Command line arg is 789.
My file directory:
project/src/m2mcom/entities/AutomatedTelnetClient.java
/web/Simple.java
/org/apache/commons/net/telnet/TelnetClient.java
The source code of the Simple.java:
package m2mcom.web;
import m2mcom.entities.AutomatedTelnetClient;
import java.util.*;
import java.io.*;
public class Simple {
public static void main(String [] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
String answer = telnet.request();
System.out.println(answer);
} catch (Exception e) {
System.err.println("Error");
}
}
}
And when I execute Simple.class, without any errors of compilation, I get this error message:
C:\Users\Victor\Desktop\project2\src\m2mcom\web>java Simple
Exception in thread "main" java.lang.NoClassDefFoundError: Simple (wrong name: m
2mcom/web/Simple)
Does anyone know how to solve this?
You're executing the command in the wrong folder, with the wrong classname. You need to use the fully qualified name (FQN) when running a Java class. And of course, you have to be in the right directory. In your example, the FQN of your class is m2mcom.web.Simple (combination of the package m2mcom.web and the simple name Simple).
As far as deducing the right directory, your classes are stored in a hierarchical folder structure, which basically starts in C:\Users\Victor\Desktop\project2\src.
So to correctly execute your program, from C:\Users\Victor\Desktop\project2\src, do;
java m2mcom.web.Simple
package m2mcom.web;
remove above line and recompile it.
when you run your code in netbeans it including in a m2mcom.web package.that is not in your class file.
So you have to be in the directory right above the package name when you execute the java command which should be in the form packagename.classname without the .class suffix.