.class file not being created in Sublime Text: Java - java

I am starting to learn Java and decided to use sublime text. I prepared a build system that would allow me to both compile and run the program. When I created my first file, it gave an error:
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
My code:
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Helloworld")
}
}
The name of this file is Helloworld.java. I noticed one thing that the directory in which I saved this file didn't have the main class file. I am confused. Please help.
Build system code:
{
"shell_cmd": "java $file_base_name"
}

javac $file_base_name (note the c) will compile your source file; java com.foo.YourClass will run it - note that the java command does not take a filename; it takes a class name.
More generally once you introduce packages and dependencies these scripts become impossible. Instead, use a build system and then if you still insist on using SublimeText (the vast majority of java programmers use IDEs such as Eclipse or IntelliJ), you can put mvn compile or mvn run in your compile/run scripts.

Related

How do I use a .jar file in my project and compile it without an IDE?

I am currently trying to include the jnativehook library and test out one of its examples.
I downloaded the .jar file from its website and created a .java file which has an example program. These are in the same folder.
I have followed other questions on here which have tried to tell me how to compile and run the program. I have tried:
javac -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample.java
java -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample
It compiles fine, however when I enter the second command to run it I get:
Error: Could not find or load main class GlobalKeyListenerExample
Caused by: java.lang.ClassNotFoundException: GlobalKeyListenerExample
All I want to do is be able to include a .jar file in my project and compile it from the command line. I am not using eclipse at the moment because I am getting too many problems with it and I also want to learn to program without IDEs.
(I am on windows by the way). Also, please do not mark this as duplicate. If it was actually a duplicate then I would be able to find a solution to my problem from the other similar questions.
Following the documentation of javac, especially "Example 3 - Specify a User Class Path" (at the bottom of the page), your java file has to have a package:
package com.example;
public class GlobalKeyListenerExample {
public static void main(String ... args) {
}
}
Then that file has to lie in the corresponding directory structure:
..something/com/example/GlobalKeyListenerExample.java
Now you can call the compiler with
javac -classpath jnativehook-2.1.0.jar;...something com/example/GlobalKeyListenerExample.java
And after compiling you can start it with
java -classpath jnativehook-2.1.0.jar;...something com.example.GlobalKeyListenerExample

Cannot run class files compiled by eclipse in command line

I compiled classes using eclipse, but when I try to run them in the command line, it returns "Cannot find or load main class".Command line.
My class is:
package Chapter10;
public class Hfpd10
{
public static void main(String[] args)
{
System.out.println("it works");
}
}
I am running the command from within the C:\Users\John\Documents\Java\EclipseWorkspace\HeadFirstDesignPatterns\bin\Chapter10 folder, where the class files are. The command is:
java Hfpd10
It runs in eclipse and I used the exact same path.
Eclipse path
The only question I could find like this went unanswered. cannot run java file in command line that created by eclipse
Change to the parent directory and run the following command:
java Chapter10.Hfdp10
This required is because your class is in a package and you need to reference it with its full package name.

java.lang.NoClassDefFoundError with jar file in terminal

In Eclipse, I wrote a Java class Test with a main() function.
The project in which is defined the class, I added the jar file bcprov-jdk15on-151.jar (I am using the library BouncyCastle).
In Eclipse, there is no problem and my program runs normally. But when I try to do it in a terminal, I get an exception.
After checking SO I found a similar post: NoClassDefFoundError while running a java program in terminal from IDE but the solution given doesn't work.
To illustrate my case, in the directory C:\Docs\workspace\Terminal\bin\ I have the file Test.class. If I run java Test I get Exception in thread "main" java.lang.NoClassDefFoundError: org.bouncycastle.math.ec.ECFieldElement.
If I run java -cp bcprov-jdk15on-151.jar Test (I put the .jar in the same directory to simplify) I get Error: Could not find or load main class Test so it seems that the dependency error is solved but another one occurs.
What am I doing wrong? Just to give the structure of my .java file:
import java.io.*;
...
public class Test {
... local methods ...
public static void main(String[] args) {
...
}
}
Thanks in advance.
Try this, you forgot to include current path "."
java -cp ".;bcprov-jdk15on-151.jar" Test
Hope it help

Java classpath confusion

I wrote and tested a small Java program using Eclipse. I'm now trying to deploy it on a Windows 7 box and Java cannot find the class. I copied the class file to C:\dxtester\classes. I'm trying to run it from the dxtester directory with: C:\dxtester>java -classpath classes;. dxtester
This produces this exception which I think I understand. Java examined the class file and is prompting me to provide the fully qualified name.
Exception in thread "main" java.lang.NoClassDefFoundError: dxtester (wrong name:
dxtester/dxtester)
If I use the FQN I get
C:\dxtester>java -classpath classes;. dxtester.dxtester
Error: Could not find or load main class dxtester.dxtester
The application is a simple test driver where everything is done in main().
package dxtester;
public class dxtester {
public static void main(String[] args) {
This seemed like an extremely simple thing to do but I'm completely baffled. What am I missing?
Your current directory is dxtester;
in this directory you have dxtester.class (I presume);
your classpath is the current directory.
This setup is wrong: your classpath must be the base directory such that package names correspond to its subdirectories. In your case you should cd to C:\ and repeat the command; ideally, however, you will have your package structure in a dedicated directory instead of the root.
I should also mention that class names should be in CamelCase.

Exception in thread "main" java.lang.NoClassDefFoundError

package pack;
public class sample{
public static void main(String input[])
{
NumberFormat numberFormat = new DecimalFormat("#,##0.00##");
System.out.println(numberFormat.format(44533125.00));
}
}
the code is working fine in the current dir.. (c:/myprogram/).
after that i copy the sample.class file and paste it in other dir(d:/myprogram).
i got error while running, like
Exception in thread "main" java.lang.NoClassDefFoundError: sample (wrong name: pack/sample)
In java .class file can run anywhere right? but why i am not able to run?
You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run
java pack.sample
(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)
If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:
javac -d classes Sample.java
or
javac -d classes src/pack/Sample.java
will (in both cases) create
classes/pack/Sample.class
You could then run
java -cp classes pack.Sample
IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it.
The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.
If you are not using a single java/class file you can also remove the package statement.

Categories