B4J Question Inline java use vairable

Shivito1

Active Member
Licensed User
Code:
B4X:
        #if java

    public static void foo() {
        try {
        @SuppressWarnings("unused")
        String command = "c:\\windows\\system32\\net.exe use f: " + x ;
Process p = Runtime.getRuntime().exec(command);
System.out.print("does not = 1");
        } catch (Exception name) {
            System.out.print("does not = 1");
}}
        #End If

Error:
B4X:
src\b4j\example\main.java:216: error: cannot find symbol
        String command = "c:\\windows\\system32\\net.exe use f: " + x ;
                                                                    ^
  symbol:   variable x
  location: class main

the Dim x as String is in the Process_Globals. Why cant I use it in the #If Java????
 

Daestrum

Well-Known Member
Licensed User
Put a '_' before the variable name
ie,
B4X:
String command = "c:\\windows\\system32\\net.exe use f: " + _x ;

All variables in Process_Globals in the output java code have a preceding underscore and are always lowercase even if defined with an uppercase name.
 
Upvote 0

Shivito1

Active Member
Licensed User
Why aren't you using jShell library?

Your code is wrong and could cause the app to freeze if the output of net.exe will be long.

How would I use jShell to do the above? I could not quite figure it out. Sorry this is my first time using b4j.
when I Googled my issue I found a java solution. That is why I did it the way I did.
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
B4X:
   Dim shl As Shell
   shl.Initialize("shl", "c:\windows\system32\net.exe",  Array("use", "f:", x))
   shl.Run(10000)
   Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
 If Success AND ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
   Else
     Log("Error: " & StdErr)
   End If
]
 
Upvote 0
Top