Recently I have discovered Sublime Text 3, but when I try to run Java with for example some println's it doesn't return anything. All it does is say "[Finished in x seconds]". Can someone please explain how to make it output text when I run my code. I am on mac os 10.9.5
Code example:
public class Main{
public static void main(String[] args){
System.out.println("Hello, world!");
}
}
That's because you are not running anything. The default java plug-in won't run code until you modify it. You are only building (compiling) your code.
To modify the plug-in you have to go to /Packages and unzip the Java.sublime-package file:
cd <sublime-text3-folder>/Packages
mkdir java
cp Java.sublime-packages java
cd java
unzip Java.sublime-packages
Then use an editor (vi, emacs...) and modify JavaC.sublime-build to add the following lines (don' forget the extra comma after the last line
{
"shell_cmd": "javac \"$file\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"variants":
[
{
"name": "Run",
"shell_cmd": "java $file_base_name"
}
]
}
Zip again the contents in Java.sublime-package and put it back in Package folder via:
zip Java.sublime-package *
cp Java.sublime-package ../<sublime-text3-folder>/Packages
Restart sublime and now along with Ctrl+B to build your project you will be able to run it with Ctrl + Mayus + B
Related
I am using an office laptop and I do not have administrative rights to it therefore I cannot edit the environment variables.
Recently, my laptop auto updated to Java 8 but something got goofed up and when ever I try to build a java program I get an error. In the meantime, I would like to build and run Java programs on Sublime text using my functioning Java 7.
How can I change the path in Sublime Text to Java 7 instead of it defaulting to Java 8 when I press CTRL-B?
Steps to use java in sublime:
In sublime go to Tools -> Build System -> New Build System
put below code:
{
"cmd": ["javac", "$file_name", "&&", "java" ,"$file_base_name"],
"selector": "source.java",
"file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)", "path" :
"C:\\Program Files\\Java\\jdk-14.0.2\\bin\\",
"shell":true
}
Save it with any name (like, Runjava.sublime-build)
Note: In path give whatever java version present in your system at these location "C:\Program Files\Java\jdk-14.0.2\bin\"
Then, create a folder with three files in it B.java(Main java file), input.txt, output.txt.
Go to Tools -> Build System -> Select Runjava
put below code in B.java file:
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
System.out.println("Hello World!");
}
}
give input in input.txt file
see output in ouput.txt file
You could try to make a special build system JavaC7 or something like that:
{
"shell_cmd": "/absolute/path/to/javac \"$file\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
I'm making a java command prompt (like the Windows CMD).
But I want to make it possible for other developers to add some commands to my program by putting their own *.jar files in a folder called "extensions".
I know this is possible because Minecraft can be modded with MC Forge like that (by putting your mod jars in a folder called "mods")
Example:
public class myExtension extends Extension {
//A method called by my program
#Override
public void onLoad() {
//Register a command in the main class...
Command cmd = Commands.registerCommand("hello");
//Add an event listener...
cmd.addEventListener(new CommandEventListener() {
public void onCommandExecute() {
//Print "Hello World !" to console when the command is executed...
Console.getConsole().print("Hello World !");
}
});
}
}
So now the guy who made this should be able to export this program as a jar file and put it in the "extensions" folder. But how can I make that my program register this jar as an extension ?
I you know the answer the please write it down ! Thank you !
Is it possible to first compile a set of Java source code files into bytecode, and later run that bytecode-- not directly, but by adding commands to another java program-- such that this new java program (in its various classes/functions) runs the previously compiled java bytecode?
If this is possible, then what is/are the required command(s) to do this?
Absolutely - and that's what libraries are all about! They're typically distributed as jar files, but you don't have to use jar files in order to reuse the code.
All you need to do is make sure that it's on the classpath at both compile-time and execution time.
For example, create the following files:
lib\p1\Hello.java:
package p1;
public class Hello {
public static void sayHi(String name) {
System.out.println("Hi " + name + "!");
}
}
app\p2\Greeter.java:
package p2;
import p1.Hello;
public class Greeter {
public static void main(String[] args) {
Hello.sayHi(args[0]);
}
}
Now let's compile our "library":
$ cd lib
$ javac -d . p1/Hello.java
$ cd ..
And now, by adding that to the classpath, we can use it in our "app":
$ javac -d . -cp ../lib p2/Greeter.java
$ java -cp .:../lib p2.Greeter Jon
Hi Jon!
(This all works on Windows with the one change of using ";" instead of ":" in the joint classpath on the last line.)
I have stuck in the class->header file for couple days!
I have tried on jni on Client by http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html and http://ringlord.com/jni-howto.html. And it succeeded in return "hello JNI C++" from JNI's (.cpp)file. Here are my steps:
create native function and in client.java
clean &build this client.java on Netbeans IDE, then result a client.class file
javah –jni [package].[classname]
create a dynamic library C++ project as first reference does, and put client.h into source file, and put some hello code into (.cpp)file ---> It works!
However, I tried to do the same thing on the servlet side and it's not working
Servlet.java->Servlet.class : ok!
Servlet.class->Servlet.h: fail!!!!
Error : cannot access javax.servlet.GenericServlet
class file for javax.servlet.GenericServlet not found
The following are solutions I have found and tried so far,
check the package name
sudo gedit /etc/profile,sudo gedit .bashrc, sudo /etc/environment; add JAVA_HOME & CLASSPATH on them, and source them to update, then echo $JAVA_HOME, echo $CLASSPATH to verify
download servlet-api-6.0.14.jar & servlet-api-5.0.16.jar from http://www.jarfinder.com/index.php/java/info/javax.servlet.GenericServlet
,and add above two (.jar) by netbeans IDE->server->property->libraries->Add JAR
Please tell me how to figure it out this issue, thank you very much!!Btw, I am using hessianServlet
NativeWrapper.java (you run javah only on this class)
class NativeWrapper {
// either
static {
System.loadLibrary("MyOpenCVNative");
}
// or
public NativeWrapper() {
System.loadLibrary("MyOpenCVNative");
}
public native void callNative();
}
MyServlet.java
class MyServlet extends javax.servlet.GenericServlet {
private NativeWrapper nativeWrapper = new NativeWrapper();
public void someServletMethod() {
nativeWrapper.callNative();
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
package in .java file makes class file unuseable
My Hellow World runs fine.
But as soon as I add a package reference I cannot run it from the command line:
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
compiles fine, then I expect to use java pv.hcw2 to run it, as:
>java pv.hcw2
>Error: Could not find or load main class pv.hcw2
I have also tried just java hcw2, to no avail.
Running in the same directory as the original which runs. Running on Windows 7 64b.
Thank you
You should have a folder called pv under which your file hcw2.java should lie. The folder pv is nothing but your package. Then outside the directory you may issue a javac command as shown below followed by java.
braga#braga-laptop:~$ javac pv/hcw2.java
braga#braga-laptop:~$ ls pv
hcw2.class hcw2.java
braga#braga-laptop:~$ java pv.hcw2
Hello Cruel World.
You have to keep your class in the folder named your package. so for :
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
The hcw2.java should be kept like pv\hcw2.java
once you compile successfully there should be the class file in same folder like :
pv\hcw2.class
While running you have to change directory to the base directory. so if your directory structure like : d:\java\pv\hcw2.java
then
Change dir to d:\java>
Run the java command there with the package name. So :
d:\java> java pv/hcw2 or
D:\java>java test.Test
Your java class needs to be inside a subdirectory called pv. So:
mkdir pv
mv hcw2.java hcw2.class pv
then you can run
java pv.hcw2