ClassNotFoundException while Running Java Application - java

I am getting a ClassNotFOundException while running a code.I am using Eclipse.
Although it has main method with proper arguments it is still not able to launch the code.
Package name and import is also correct.
public class TestNew {
public static void main(String[] args)
{
ArrayList<String> sumList=new ArrayList<String>();
Cm cm=new Cm();
sumList=cm.sumListCombo();
for(int i=0;i<=sumList.size();i++)
{
System.out.println(sumList.get(i));
}
}
}
Import of respective packages are done.

Try refreshing the Project, then clean it and build it. If there is no problems, your code will work. If there is a problem, it will be some kind of 'Organize Imports' issue. Ctrl+Shift+o and you are ready.

Try Ctrl + Shift + O if you're on windows.
This organizes your imports(adds missing ones and removes unnecessary ones).
Then on the menu bar, Project >> Clean...
Then re-launch your app.

Related

Eclipse autocomplete broken between certain lines of code

I've been having issues with Eclipse's autocomplete for a while, as it stops working halfway through coding a project. After lots of unsuccessful debugging, I tried narrowing down my code and have the resulting bare-bones example of my file (minus lots of other functions):
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class main {
public static void main(String[] args) {
List<String> zlist = new ArrayList();
zlist.forEach(fileName -> { // CONTENT-ASSIST BREAKS HERE
String test = "";
if (true) { // CONTENT-ASSIST WORKS RIGHT AFTER THIS LINE
}
});
Iterator<String> iter = null;
while ("".equals("")){}
}
}
For some reason, none of the content-assist (Ctrl+Space) works between the forEach and the if statement. Commenting out either Iterator or while loop line makes it work again, but I need that current setup. The file runs fine without errors too. Is there a reason why this occurs, and is there a proper fix?
I have checked: Window > Preferences > Java > Editor > Content Assist > Advanced and Java Proposals is checked.
Booting up Eclipse in -clean mode didn't help, nor did cleaning or rebuilding the project.
The issue occurs even in a new workspace and when I create a new file with the same code (it being the only file in the project).
My setup is: Eclipse Java EE IDE for Web Developers, Neon.1 Release (4.6.1). Windows 7 64-bit.
Here's some screenshots of the errors:
The last screenshot shows it working when I comment out the Iterator declaration at the bottom. Not quite sure why that affects the autocomplete.

Bytecode Hotswap - perplex

I have no idea anymore. Since today 14pm the hot-code replace does not work anymore.
I had jdk8_74x64 installed on win10x64.
I test it using this code in a simple project (without maven and without dependencies):
public class Test{
public static void main(String[] args){
System.out.println("hi"); // breakpoint here, it stops always
System.out.println("ho"); // on suspending i change this to "hoy".
}
}
Start eclipse with new workspace - still not working
Restart computer - still not working
Uninstall all JDK/JRE and remove all javaws.exe, install again - still not working.
Uninstall eclipse, reinstall eclipse - still not working
Installed netbeans, using "apply code changes" - still not working.
Delete jdk with all javaws.exe, delete eclipse, uninstall netbeans, use new user, install jdk 8u31x64 install netbeans - still not working
Installed idea-community-edition15+jetbrains, change ho to hoy and apply class-changes (idea notifies me 1 class reloaded) - still not working.
How to get hot-code-replace working?
Hm,
public class Test{
public static void main(String[] args){
System.out.println("hi"); // breakpoint here, it stops always
System.out.println("ho"); // on suspending i change this to "hoy".
}
}
does not work but
public class Test{
public static void main(String[] args){
foobar();
}
public static void foobar(){
System.out.println("hi"); // breakpoint here, it stops always
System.out.println("ho"); // on suspending i change this to "hoy".
}
}
Works great everywhere.

Can't run any application with Intellij IDEA

I used to work with Intellij IDEA and at a certain moment something happened that i can't run any application anymore. The problem is that buttons 'Run' and 'Debug' (the ones that are called with Shift+F10 and Shift+F9) are inactive -
.
There are also another pair of this buttons, that are called with Alt+Shift+F10(F9), but after i click on them, i get this -
.
I tried to reinstall IDE, but that didn't help. Here the code of the application i'm trying to run, though it doesn't work with any application.
P.S. I'd like to add also a screenshot of the project structure, but i can't post more that 2 links, so i just do this way:
>untitled
>idea
>out
>src
>(C) WallyFinder
untitled1.iml
>External Libraries
The code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class WallyFinder {
public static int wheresWally(String str) {
Matcher m = Pattern.compile(" Wally\\b").matcher(" " + str);
return m.find() ? m.start() : -1;
}
public static void main(String args){
int a = WallyFinder.wheresWally("aaaaWally");
System.out.println(a);
}
}
The directory structure is wrong I think. It should be like this
>untitled
>idea
>out
>src
>main
>java
>(C) WallyFinder
untitled1.iml
>External Libraries
Can you please try this.

Eclipse Mars (4.5) hot swap code in debugger not working

So I updated to Eclipse Mars (4.5) and for some reason I'm unable to use the hot swap code in the debugger. Normally I could do something like this:
public static void main(String[] args){
while(true){
System.out.println("123");
}
}
Then if I started it in debug mode, changed the text to "321", then save, then it would update without the need for restarting it. It behaves exactly like it was run in "Run" mode instead of "Debug".
What I have tried:
Creating a new workspace, creating a fresh project, using the code above, nothing happens
Have several JDKs installed, have tried with java 6, 7 & 8, changed the workspace and/or the project settings to use the different JDKs, nothing happens (the fact that I have several versions of java installed shouldn't matter as it was just the moment I updated eclipse it stopped working)
Tried uninstalling removing any config files to eclipse (on a mac, so that would be every file/folder with the word "eclipse" in the ~/Library folder, ran a "find" search to detect all the files). Then tried to create a new workspace, now project, the code snipped, ran in debug mode, nothing happens on save.
Have also made sure I have "Auto Build" enabled, even tried to "clean" it, and disable auto build, then save the code, then do a manual build while the debugger was running: nothing happens
I'm starting to get desperate as I have a hard time getting work done without having debug mode available so any help/hints in the right direction would be of much appreciation.
HotSwap doesn't work with static methods. However it works fine with instance methods, so it will work on this code:
public class Main {
public static void main(String[] args) {
new Main().f();
}
public void f() {
while(true){
System.out.println("123");
}
}
}
Ok so I finally found the problem. It seems that you can't edit loops while they are running. Say you have a loop like this:
public static void main(String[] args){
while(true){
System.out.println("123");
}
}
Then you can't edit the "123" string.
You can how ever edit methods which are called inside the loop like this:
public static void main(String[] args){
while(true){
System.out.println(methodA());
}
}
public static String methodA(){
return "123";
}
Now you can edit the string "123" and it will update.
This also applies for infinite "for" loops, so guess the rule of thumb is that the method body has to be "re-called" before updating, and it isn't enough to wait for the next loop call.

Error: Could not find or load main class hello.world.HelloWorld

I am trying to run this project called "hello user". I am new to Java, so wrote a simple program that takes your name, and displays "Hello ". while Running it, I get the following error:
run:
Error: Could not find or load main class hello.world.HelloWorld
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
But when I run file HelloWorld.java, it does it fine
I am doing this on Netbeans IDE 7.2
Rather than the coding error, it could be related to IDE. Since the "Run File" runs okay, but 'Run Project" does not, I believe you have something to set up in IDE itself. Right click the project, and select "Set is as Main", now run the project. I am just giving it a guess, may not help you. But it worth a shot.If it does not help, please paste your code too.
Your class needs a public static void main(String[] args) function. And moreover I suspect that the error could be in the package.
If you want your class in <main_package>.<sub_package>, The directory structure is
- main_package
- sub_package
-HelloWorld.java
And be sure to write your class like this.
package main_package.sub_package;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello " + args[o]);
}
}
This is all due to the naming convention in Java
You need to run the .class file containing the public static void main(String[] args) method..
Here, your HelloWorld.java file might contain a class with main() method.. So, you can run it..
This is because, execution of any Java program starts with the invocation of main().. JVM needs an entry point to your code.. Which is main().. If it doesn't find one.. It will not run..
So, make sure, whatever class file you are running, it should have main() method..
UPDATE :- And for the starting point, may be you can skip using packages.. Just go with plain Java class without packages..
This message can also appear in Eclipse (Juno 4.2.2 in my case) and I have found two potential causes for it.
In my cases:
1. a DTD was in error. I deleted the file and that solved the issue*.
2. having cleaned the project, an external Jar that I had built externally had been deleted as could be seen from Properties -> Java Build Path -> Libraries.*
*Having solved either of the above issues, it was necessary to restart Eclipse
if you are using intellij idea then just rebuilding (clean and build) project might solve your problem . because intellij might be still trying to load the old classes which are not there or changed
Make sure you call looks like below:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello user");
}
}
To run a Java class in stand alone mode, public static void main(String[] args) is the entry method, which is must.

Categories

Resources