This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 9 years ago.
Im a java noob. Basically I am trying to create a program that compares two command line arguments while ignoring case and prints out the lesser of the two strings. Here is what I have so far:
public class CompareStrings
{
public static void main(String[] args)
{
String s1 = new String(args[0]);
String s2 = new String(args[1]);
if ( s1.compareToIgnoreCase(s2) > 0 )
System.out.println(s2);
else if ( s1.compareToIgnoreCase(s2) < 0 )
System.out.println(s1);
else
System.out.println("Both strings are equal.");
}
}
I keep getting the error
Error: Could not find or load main class CompareString
when I try to run it. What am I doing wrong?
"Error: Could not find or load main class CompareString"
Your error message says you couldn't load class "CompareString", but your code says your class name is CompareStrings.
Your class name is wrong.
Your error says
Error: Could not find or load main class CompareString
but the name of class is CompareStrings not CompareString
launch using java CompareStrings
Read this good tutorial on compiling and launching java programs
First of all you are trying to use wrong class, should be CompareStrings and not CompareString.
Second, I would recommend using a nice utility lib for handling command line called Cliche from Google Code site
And third, it would be good to check if the given string is null before you call compareToIgnoreCase on it
as per your error i can gues that you are running CompareString but your class is CompareStrings
So either run java CompareStrings or rename your class to CompareString
If this is the only code you have, save that file as CompareStrings.java not CompareString, and in command prompt javac CompareStrings.java. (to do this you need to configure java). then use java CompareStrings abc cbs to run this. and this will give you out put as abc
Related
I'm learning Java through the this Princeton course https://algs4.cs.princeton.edu/home/. I've create a simple Java program called Ortho.java using the Java provided by the course (that I installed using this tutorial: https://algs4.cs.princeton.edu/linux/). The program receives two .txt input files, it reads those as a list of strings, and print their first value.
Here's the program:
package edu.princeton.cs.algs4;
import java.util.Arrays;
class Ortho{
public static void main(String args[]){
// Read files from input and print statements
In in = new In(args[0]);
String[] list = in.readAllStrings(); //parse the first text input
while (!StdIn.isEmpty()) {
String key = StdIn.readString();
System.out.print(key);
}
}
}
I run this command on the shell to compile my program, this creates a file called Ortho.class:
javac-algs4 Ortho.java
This is the command I use to run the program:
java-algs4 Ortho.class text_1.txt < text_2.txt
But I always get this error:
Error: Could not find or load main class Ortho.class
Caused by: java.lang.ClassNotFoundException: Ortho.class
What do I need to do to properly compile and run this program with the algs4 Java lib provided by the course?
This is my project structure:
dir/
Ortho.java
Ortho.class
text_1.txt
text_2.txt
Run the program like this :
java algs4.Ortho text_1.txt < text_2.txt
Let me know if this worked for you or no.
This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 6 years ago.
Lets say i have this String: String run = "System.out.println\(\"Hello\"\)". What i want to do is run what is in the string to output Hello in console.
Maybe there is a method like String.run()?
Try BeanShell , build your app with jar library.
import bsh.Interpreter;
private void runString(String code){
Interpreter interpreter = new Interpreter();
try {
interpreter.set("context", this);//set any variable, you can refer to it directly from string
interpreter.eval(code);//execute code
}
catch (Exception e){//handle exception
e.printStackTrace();
}
}
Maybe in Java 9 you could use the REPL but as it's not there yet You would need to
* create a temporary file with a class with a know to You API
* run javac on it and compile it
* load the compiled class with a class loader
* run the code
If You want to do is running dynamically defined scripts in Your code then You could use Nashorn and JavaScript. It would do what You want. Also You could use Groovy in your project instead of Java - the syntax is similar to Java but Groovy is a dynamic language.
No, you cannot do it and there's no method to run this command in String. Anything withing the double quotes becomes String literals only and compiler doesn't take care of any command written in that.
This question already has answers here:
How can you run a Java program without main method? [duplicate]
(4 answers)
Closed 8 years ago.
Is it possible to run a java program without main method ?
How can I execute this Java program in eclipse?
public class A {
static {
System.out.println("hello");
System.exit(0);
}
}
There are some ways to "run a Java program" without main method.
For example:
you could create a jUnit test. In that case Eclipse will offer you the option "Run as jUnit Test".
you can create a class which extends JApplet. In that case Eclipse will offer the option "Run as Java Applet".
Your question is copied from here, but the answer is :
Use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
or
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
The static method main, which receives an array of strings. The array should have two elements: the path where the files are located (at index 0), and the name of the files to process (at index 1). For example, if the name was “Walmart” then the program should use “Walmart.cmd” (from which it will read commands) and “Walmart.pro” (from which it will read/write products).
I don't want anyone to write the code for me because this is something I need to learn. However I've been reading this through and the wording is confusing. If someone could help me understand what it wants from me through pseudo-code or an algorithm it would be greatly appreciated.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
The main method's String array input argument consists of whatever String arguments you pass to the program's main method when you run the program. For example, here is a simple program that loops over args and prints a nice message with each argument's index and value on a separate line:
package com.example;
public class MainExample {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.printf("args[%d]=%s\n", i, args[i]);
}
}
}
Once you've compiled the program, you can run it on the command-line and pass it some arguments:
java -cp . com.example.MainExample eh? be sea 1 2 3 "multiple words"
Output:
args[0]=eh?
args[1]=be
args[2]=sea
args[3]=1
args[4]=2
args[5]=3
args[6]=multiple words
So lets explain to you
Create a class Inventory : if you don't know how to create a class google it just as is
The static method main: Every executable class in java (at least from the console) has the main method you should google java main method and propably in the same place you find it you will see the default arguments that it receives
When you learn about the default arguments of method main you will undertand about the 'args' that has to be on it
You will have t study the class String google it "java String class"
You will have to study the class File google it "java File class"
At the end everything else would be just logic and I beleave you have learned some at this point.
public class Inventory { // class inventory
public static void main(String[] args) // main method
{
if(args.length==2){ // check if args contains two elements
String filePath = args[0];
String fileName = args[1];
filePath+= System.getProperty("file.separator")+fileName;
File fileCMD = new File(filePath+".cmd");
//fileCMD.createNewFile();
File filePRO =new File(filePath+".pro");
//filePRO.createNewFile();
}
else {
//write the code to print the message Usage: java Inventory Incorrect number of parameters for a while and exit the program.
}
}
This is what I've understood. Basically you have to write a program to create two files, one called fileName.cmd and the other fileName.pro. You have to construct the path of the files using the arguments (input parameters of the main method) and system's file separator. If the arguments don't have two elements you have to print the 'invalid' message. That's it.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
You have to use command line to pass the arguments and launch the program , something like the following code in cmd or terminal:
java inventory thePath theFileName
That's how it get initialized.
Hi i am a java learner and trying to make this program for add two number.
While running this i am getting this error msg..
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at addnumber.main(addnumber.java:16)
Java Result: 1
public class addnumber{
public static void main(String[] args) {
String x,y;
int a,b,c;
x=args[0];
y=args[1];
a=Integer.parseInt(x);
b=Integer.parseInt(y);
c=a+b;
System.out.println(c);
}
}
I know i can use Scanner class or string builder class however whats wrong with this code?
If you use the args-array you have to give the programm some parameters from outside for example from the console.
So open the console and go to the directory where the .java file is and compile it manually with
javac Addnumber.java
Now you should see a .class file there.
Than write a call like this:
java Addnumber 5 9
your arguments would be 5 and 9.
Also write the classname in capitals
If your running with eclipse or any other working tool. then you have to set run configuration-arguments.
Suppose you run this code in command line then you have to run this code using the below command
java addnumber 2 4
Good Quastion, The Problem is in String[] args, this problem leads to find out what is the initial value of this Parameter as string? or in other words How is the IDEs call the main method? if you run this code in Netbeans or Eclipse by default will print this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
because by default the IDE call method with array of string with 0 length:
TheNameOfClass.main(new String[0]) // if you run it in some IDEs
>java Addnumber // cmd with no args?
In your case the exception will occur in line x=args[0]; because you invoke and you want to use some item in array out of range or grater than the length of the array.
Now you can configure it as example in eclipse to pass some Strings values as you need OR you need to compile and run the java class manual in a 'Command Prompt' and passed some of Strings values:
>javac Addnumber.java // compile it
>java Addnumber 1 77 // run it and passs some values to array in main method