How can I change the path to Java in Sublime? - java

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"
}

Related

How to run a java jar file and get store the output in shell script variables

I have a java class, which has 3 methods and each method returns a String. I exported the .jar file out of that Java project. When I run the jar file from git bast using the command ./script1.sh, I get the output in the command window. My question is, how can I assign those values to the three variable that are in the script1.sh file. If I type echo ${"Source_branch"}, i should get output as "XXX_Source_Branch".
Here is my Java code :
package com.main;
public class Engine {
public static void main(String[] args) {
try {
getSourceBranch();
getDestinationBranch();
getDestEnv();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getSourceBranch() {
return "XXX_Source_Branch";
}
public static String getDestinationBranch() {
return "XXX_Dest_Branch";
}
public static String getDestEnv() {
return "XXX_Dest_Env";
}
}
The name of the jar file is Engine.jar.
And my script file is
runjar.sh
java -jar Engine.jar
Source_branch
Destination_brnach
Destination_env
pass data from java to bash
If you want to save data from java to the overlaying String, you will need to print that to System.out and read them from the shell.
Java code:
System.out.println("your source branch")
System.out.println("your destination branch")
Shell code
out="$(java -jar Engine.jar)"
sBranch="$(echo "$out"|sed -n '1p' /dev/stdin)"
destBranch="$(echo "$out"|sed -n '2p' /dev/stdin)"
pass data from bash to java
If you hard-code the Strings, java will just use the Strings. If you want to change that, you will need to modify your java code.
option 1 - arguments
If you want to pass the strings in the script using java -jar Engine.jar Source_branch Destination_brnach Destination_env, you can use them in your main method with the args parameter.
For example, you can print the first String in your main method using
System.out.println(args[0]);
If you do that, please test if args.length is greater than the highest argument you are accessing.
option 2 - System properties
If you want to get parameters without accessing args(independent of your main method), you will need to use System properties.
At first, change the script to be like that:
java -jar -DSource_branch=yourSourceBranch -DDestination_branch=yourDestinationBranch -DDestination_env=yourDestinationEnv
Engine.jar
Note that -D. That indicates that you are passing System properties. The syntax for this is -D<key>=<value> and it should be located between java and -jar.
Also, you will need to use System.getProperty() to access it from anywhere in your code.
For example:
public static String getSourceBranch() {
return System.getProperty("Source_Branch");
}
Create a .sh file with any file editor, like-
$> vi runJar.sh
Then, paste the below script ,
#!/bin/sh
java -jar Engine.jar
After saving the file, give execution permission -
$> chmod +x runJar.sh
Then run using
$> ./runJar.sh
Make sure the jar file and shell script file are in the same directory.

Connecting java with constraint logic programming

i'm trying to connect java with constraint logic, i'm using netbeans for java and eclipse 6.1 for constraint logic, but when i'm trying to run the code there is an exception appears java.lang.IllegalArgumentException: Missing eclipse.directory property
i've used a tutorial that explain how to connect them, which says that After compilation, to run the program, start the Java interpreter as you normally would but before the name of the class, supply the command line option
-Declipse.directory=<eclipse_directory>
and i don't know where to place it in netbeans
here is the code
import com.parctechnologies.eclipse.*;
import java.io.*;
public class eclipseConnection {
public static void main(String[] args) throws Exception
{
try{
EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions();
EclipseEngine eclipse;
eclipseEngineOptions.setUseQueues(false);
eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions);
eclipse.rpc("write(output, 'hello world'), flush(output)");
((EmbeddedEclipse) eclipse).destroy();
}catch(Exception e){
System.out.println(e);
}
}
}
You can add the property definition in the 'Run' menu: Run > Set Project Configuration > Customize.... Make sure you enter the property definition -Declipse.directory=<eclipse_directory> in the VM Options section.
Let's use the command line and the example source file Quicktest.java.
Copy the example:
copy "C:\Program Files\ECLiPSe 6.1\doc\examples\JavaInterface\Quicktest.java" .
Compile it:
javac -classpath "C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" QuickTest.java
Run it:
java -classpath ".;C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" -Declipse.directory="C:\Program Files\ECLiPSe 6.1" QuickTest
hello world

Sublime text 3 how to: Java output

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

class file for javax.serverlet.GenericServlet not found

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();
}
}

Having trouble starting a program with the Runtime.getRuntime().exec(command)

I'm currently writing a Java program that can open .exe programs on my PC, like MS Word for example.
I am having a problem though, because Runtime.getRuntime().exec() will only successfully open certain programs. I have used the exact same code for all the programs, but regardless, some programs won't open.
Here is my code for a program I downloaded, Picasa 3:
class picasaHandler implements ActionListener
{
public void actionPerformed(ActionEvent r)
{
try
{
Runtime.getRuntime().exec("cmd /c start Picasa3.exe");
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find Picasa 3");
}
}
}
So my question is, why won't Runtime.getRuntime().exec() run all the programs I use it on, and how do I run programs like Picasa 3, that I cannot run at this moment with this method.
I'm guessing that Picasa3.exe is not on your %PATH% anywhere so it doesn't know how to load it. Have you tried specifying the full path to Picasa3.exe?
Runtime.getRuntime().exec("cmd /c \"c:\\program files (x86)\\Google\\Picasa3\\Picasa3.exe\"")
File file=new File("picassa3");
String filename=file.getAbsolutePath(file);
try
{
Runtime.getRuntime().exec(filename);
}
catch (IOException t)
{
JOptionPane.showMessageDialog(null,
"Sorry, could not find the file");
}
Runtime's exec can only start applications that are on the Windows path. Some programs are automatically on the path, while others, like Picasa, is not.
The only work-around for this is to determine the correct path and then launch that application.
This might work for you.
If you want to run a certain program using Runtime.exec(), just add it's installation path to path variable in your System Variables. To find it's installation path, simply right click on it's shortcut and select "Find Target". Then concat that entire address at the end of your path Variable.

Categories