So I followed this site here and I don't think the part where I went wrong was the first part when it was talking about directories and stuff. I'm a real noob. Anyways, when I try to run the java program, it says Error: Could not find or load main class Mainclass. Any idea what's going on? I've tried installing the java jdk but that doesn't make a difference.
Details:
Here's a screenshot of what my page looks like.
Is there something I should change in my .build file? (I used the same one on the site). Thanks in advance.
You need to have a class called Main (or main) with a method like this one:
public static void main(String [] args){
//your main class code here
}
EDIT:
Try renaming your class to "Main" (no quotes).
Large Sized EDIT:
1.You need to make your class public class MainClass
2.You MUST have a package declaration!!!
3.You SHOULD probably rename your class to "Main" and your java file to "Main". Other than that, your programming is pretty solid!
Try below code:
sudo apt-get update
sudo apt-get install default-jdk
I cannot see your code but this is a sample code:
class greeting {
public static void main(String args[]) {
System.out.print("hi");
}
}
Happened to me recently, the Class file saved in your Cloud9 folder has a lower case letter, different from the Class name uppercase
Filename: Mainclass
ClassName: MainClass
Related
I am learning how to use packages in Java, but I am running into trouble when trying to implement them. I have a simple class called Main which appears as follows:
public class Main
{
public static void main(String[]args)
{
System.out.println("Package Test...");
}
}
The directory of this class is: C:\Users\MyComputer\Desktop\Packages\Main.java
When I compile this class, I run into no trouble. However, when I add "package com.example.mypackage;" to the top of the .java file, compile the program, and try to run the program, I receive the following error: "Error: Could not find or load main class Main"
What can I do to solve this problem?
If the path of your class is C:\Users\MyComputer\Desktop\Packages\Main.java then your class is not in a package. In this instance "Packages" is your project folder, and it only contains the one java class.
If you want package com.example.mypackage; to work, then your path needs to be:
C:\Users\MyComputer\Desktop\Packages\com\example\mypackage\Main.java
I use soot for instrumenting a java program. I know for adding invocation to specific class in soot, we must set "Soot class-path" to the directory contains that class, .class file. So I do this in main method of main class. I bring the snippet of code bellow
public class Main {
public static void main(String[] args) {
Scene.v().setSootClassPath("/home/j/IdeaProjects/Test_1/classes:/home/j/IdeaProjects/Test_1/libs/rt.jar:home/j/IdeaProjects/Test_1/libs/jce.jar");
PackManager.v().getPack("jtp").add(new Transform("jtp.RetIns", new ExIns()));
....
But when I want to use "Insop" class which resides in classes folder, by following code in Exins method:
static SootClass Ins;
static
{
Ins= Scene.v().loadClassAndSupport("Insop");
}
I get the error
Caused by: java.lang.RuntimeException: couldn't find class: Insop (is your soot-class-path set properly?)
I should mention that I use ubuntu 14.4 32 bit and I run the code on intellij.
I can not find what is my mistake. could you please help me.
I finally find the problem. I don't know why, but I should set "soot class-path" with relative path. For my project for example it should set as follow:
Scene.v().setSootClassPath("classes:libs/rt.jar:libs/jce.jar");
filename: mainoverloading.java
error: could not find or load main class mainoverloading
class simple{
public static void main(int a)
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("Hi");
main(10);
}
}
Your class is named simple (not mainoverloading). Rename the class (or move the file "mainoverloading.java" to "simple.java").
When You compile the above given class using
javac mainoverloading.java
It is successfully compiled and a class file named simple.class is generated in your folder.
You can then run it by typing
java simple.
But this is actually not a good practice, as Elliott Frisch said rename your class into simple.java.
have a look For Windows- HelloWorld
For Linux - HelloWorld
Java allow us to use any name for file name, only when class is not public.
Its running nicely, because for eclipse main class it simple, its smart to identify that simple.class will be created.
If you are running from command line ,
class file created for your code is simple.class so JVM will be unable to find mainoverlading.class
When I want to run my java code, I have this problem :
Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
at com.ibm.icu.text.BreakDictionary.main(BreakDictionary.java:44)
i can't run my code... It's simple
public class main{
public static void main(String[] args){
System.out.print("Hi");
}
}
WTF is com.ibm.icu.text.BreakDictionary?
Make sure of the class whose main you are running. I suspect you're not running your class. Your java command should look like this:
java -cp somepath com.mycompany.mypackage.MyClass
Better yet, use an IDE like eclipse that make it easy to run a class
Also, rename your class from main to MyClass - anything with a leading capital letter - it's good coding style
Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
at com.ibm.icu.text.BreakDictionary.main(BreakDictionary.java:44)
This exception says that you are running the main method in a class named BreakDictionary. In your sample code the class is called main.
You can solve this in eclipse by right-clicking your class and select run as and then java application. This will ensure that you are using your main class as the entry point and not BreakDictionary (which seems to either be on your classpath or you are simply not running the correct project in eclipse).
I'm getting Java code generated for me from an application.
I took the JRE and extracted all the files into a new directory.
There is a META-INF folder that has a MANIFEST.MF without any main method.
Witin this JRE is the class of the code I'm interested in however when I CMD the following...
java Steve.class
I get this error...
Could not load for find Main Class Steve.class.
I'm assuming that somewhere in all these class files there is a Main class but how do I search all these documents to find it? Is there an application?
Thanks!
You don't need the .class suffix when invoking a Java program. Do it like this:
java Steve
To work out which class has a main method, you can use javap (Java Class File Disassembler) on each class file. For example:
$ javap Foo
Compiled from "Foo.java"
public class Foo extends java.lang.Object{
public Foo();
public static void main(java.lang.String[]);
}
First: every class that exposes this method signature:
public static void main(String[] args) { }
could be a main class launchable from the JVM and eligible to be put in the manifest to enable shell execution.
Second: when you launch a class in a JRE you must specify the fully qualified name of the class; for example if Steve.class file is in a tree structure such as com/mycompany/app, starting from the root of your application where the MANIFEST directory is, you should launch it, from the root directory, typing:
java com.mycompany.app.Steve
So if Steve exposes a main method and if you can correctly point to it from the root, you can launch it.